2017-02-15 46 views
4

我使用an emogrifier來格式化刀片模板的結果,以便我可以通過電子郵件發送它。乾淨地emogrify Laravel刀片模板文件的最佳方法是什麼?

$html = View::make('emails.notification', [ 
    'data' => $data 
]); 

$emogrifier = new Emogrifier($html, null); 

$result = $emogrifier->emogrify(); 

// now I can send the result in an email... 

可正常工作,但在乾淨的代碼和可測試性的興趣,我想延長我的刀模板,模板本身emogrify HTML。事情是這樣的:

@emogrify 
    <style> 
    .red-text { 
     color: red; 
    } 
    </style> 
    <p class="red-text">This text is red</p> 
@endemogrify 

...但它看起來像刀片指令不允許打開/關閉標籤這樣的。有沒有更好的方法來完成這一點?

+0

如果你發現任何有趣的解決方案我很感興趣。 – AsTeR

+1

@AsTeR我從來沒有找到一個優雅的解決方案。我只是在刀片模板外面模糊了。 – Divey

+0

我最終也這樣做了! – AsTeR

回答

-1

這裏是我怎麼做我的電子郵件刀片laravel(我一般不使用emogrifier,但希望這有助於):

創建基礎電子郵件刀片(通常在資源/視圖/電子郵件/ email.blade.php):

<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
    <meta charset="utf-8"> 

    <style> 
     /* Include styles for all emails here */ 
    </style> 

    @section('head') 
    @show 

</head> 

<body> 

<div class="email-body"> 
    @section('body') 
    @show 
</div> 

</body> 
</html> 

然後,當我想創建一個電子郵件,我延長了刀片:

創建NE在資源/視圖/電子郵件黑白文件/新用戶email.blade.php:

@extends('emails.email') 

@section('head') 
    <style> 

     /* Add additional styling here if you need to! */ 

    </style> 
@stop 

@section('body') 

    <!-- Email body content here! --> 
    <h1>Welcome new user!</h1> 

@stop 
+0

這並不能真正回答我的問題。我特別想知道如何使用修改HTML部分的自定義指令來擴展Blade。或者如果還有其他一些乾淨的解決方案,我就會全神貫注。 – Divey

+0

你看過[擴展刀片](https://laravel.com/docs/5.4/blade#extending-blade)來添加自己的指令嗎?看起來好像你將不得不渲染刀身,然後應用Emogrifier。我只是提供了一個替代方案,就像你問「如果有更好的選擇。」我發現定義基於電子郵件使用的樣式的基礎電子郵件刀片通常更容易依賴於Emogrifier。 –

相關問題