2013-01-17 47 views
9

ZF1 में मैं एक मेल शरीर प्रस्तुत करना के लिए निम्न कोड का प्रयोग किया:ZF2 में लेआउट के साथ मेल टेम्पलेट कैसे प्रस्तुत करें?

// View erstellen 
$view = new Zend_View(); 
// Layout erstellen 
$layout = new Zend_Layout(); 

// HelperPath muss hier nochmals übergeben werden da es ein neues View Objekt ist. 
$view->addHelperPath('Own/View/Helper', "Own_View_Helper_"); 

// ViewScript 
$view->setScriptPath(APPLICATION_PATH . '/views/scripts/emails/'); 

// LayoutPath 
$layout->setLayoutPath(APPLICATION_PATH . '/layouts/scripts/'); 

$layout->setLayout('layoutMail'); 
$layout->setView($view); 

foreach ($assigns as $key => $value) { 
    $view->assign($key,$value); 
} 

$layout->content = $view->render($templateName); 
return $layout->render(); 

मैं बहुत कोशिश की, लेकिन मैं ZF2 में इस समारोह का एहसास नहीं कर सकते हैं। मेरा वास्तविक कोड यह है। लेकिन यह मानक लेआउट का उपयोग करता है और मैं इसे एक स्ट्रिंग में नहीं प्राप्त कर सकता।

public function mailAction() 
{ 
    $viewModel = new ViewModel(); 
    $viewModel->setTemplate("zhorty/test"); 
    $viewModel->setVariable('test', 'some value'); 
    return $viewModel; 
} 
+0

किस कारण से आपको ** लेआउट ** की आवश्यकता है? आम तौर पर एक ** दृश्य ** के साथ एक ईमेल के शरीर का प्रतिपादन पर्याप्त है। एक लेआउट (इसलिए एक दृश्य दृश्य encapsulating) ईमेल के लिए afaik overdone है। –

+1

@ जूरियन मुझे लगता है कि यह लेआउट हेडर और फ़ूटर को स्टोर करने के लिए एक अच्छी जगह है, जैसे लोगो और अस्वीकरण, जो प्रत्येक ई-मेल के लिए समान हैं। चीनी में एक लिंक के लिए – Lauris

उत्तर

6

उम्मीद है कि अपने ब्लॉग पोस्ट में से एक तुम बाहर में मदद मिलेगी, इसके बारे में how to use Zend\View as template in Zend\Mail and add attachments in ZF2

यह चीनी ने लिखा है, लेकिन मैं अभी-अभी पढ़ा लगता है कि कोड काफी स्पष्ट है। इसके अलावा आप read it with help of Google translation

+0

प्लस 1! –

10

धन्यवाद! आपके जवाब ने मेरी मदद की!

मैंने कोड बढ़ाया। तो मैं एक लेआउट टेम्पलेट का भी उपयोग कर सकता हूं।

$view = new \Zend\View\Renderer\PhpRenderer(); 
$resolver = new \Zend\View\Resolver\TemplateMapResolver(); 
$resolver->setMap(array(
      'mailLayout' => __DIR__ . '/../../../../Application/view/layout/layout-mail.phtml', 
      'mailTemplate' => __DIR__ . '/../../../view/zhorty/test.phtml' 
    )); 
$view->setResolver($resolver); 

$viewModel = new \Zend\View\Model\ViewModel(); 
$viewModel->setTemplate('mailTemplate') 
    ->setVariables(array(
    'test' => 'AlloVince' 
));  

$content = $view->render($viewModel); 

$viewLayout = new \Zend\View\Model\ViewModel(); 
$viewLayout->setTemplate('mailLayout') 
    ->setVariables(array(
      'content' => $content 
)); 

echo $view->render($viewLayout); 
+0

यह उत्तर पूरा नहीं हुआ है, कृपया पूरा कोड के लिए मेरा उत्तर देखें –

7

मेरा जवाब user1986560 के जवाब और How to Render a custom view with plugins in Zend Framework 2 पर आधारित है। मैं इसे जोड़ रहा हूं क्योंकि मुझे लगता है कि यह चीजों को लागू करने के लिए थोड़ा आसान बनाता है।

मेरे पास एक ईमेल लेआउट और विभिन्न सामग्री फ़ाइलें हैं। लेआउट का पुन: उपयोग किया जा सकता है और विभिन्न सामग्री फ़ाइलों को जोड़ा जा सकता है।

देखें/ई-मेल/layout.phtml

<table> 
    <tr><td><img src="header.png" /></td></tr> 
    <tr><td><?= $this->content; ?></td></tr> 
    <tr><td><img src="footer.png" /></td></tr> 
</table> 

देखें/ई-मेल/contact.phtml

<h1>Contact us email</h1> 
</ br> 
Name: <?= $this->name;?></ br> 
Email: <?= $this->email;?></ br> 
Message:<?= $this->message;?></ br> 

अपने मॉड्यूल conf में, लेआउट और अलग अलग सामग्री फ़ाइलें जोड़ । इसे इस तरह से करके, आप व्यू हेल्पर्स का उपयोग कर सकते हैं।

module.config.php

'view_manager' => array(
    'template_map' => array(
     'email/layout' => __DIR__ . '/../view/email/layout.phtml', 
     'email/contact' => __DIR__ . '/../view/email/contact.phtml', 
    ), 

अपने नियंत्रक/कार्रवाई में:

// View renderer 
$renderer = $this->getServiceLocator()->get('Zend\View\Renderer\RendererInterface'); 

// Email content 
$viewContent = new \Zend\View\Model\ViewModel(
    array(
     'name' => $name, 
     'email' => $email, 
     'message' => $message, 
)); 
$viewContent->setTemplate('email/contact'); // set in module.config.php 
$content = $renderer->render($viewContent); 

// Email layout 
$viewLayout = new \Zend\View\Model\ViewModel(array('content' => $content)); 
$viewLayout->setTemplate('email/layout'); // set in module.config.php 

// Email 
$html = new MimePart($renderer->render($viewLayout)); 
$html->type = 'text/html'; 
$body = new MimeMessage(); 
$body->setParts(array($html)); 
$message = new \Zend\Mail\Message(); 
$message->setBody($body); 
-1

@ user1986560 जवाब है, पूरा नहीं हुआ है क्योंकि आप "यूआरएल की तरह कुछ बुनियादी सहायक की जरूरत है चाहिए "राउटर और कभी-कभी" बेसपाथ "के साथ भी आवश्यक है।

$myApp=Zend\Mvc\Application::init(require 'config/application.config.php'); 
$sm=$myApp->getServiceManager(); 

$view=new \Zend\View\Renderer\PhpRenderer(); 
$view->getHelperPluginManager()->setServiceLocator($sm); 
$basePathX='/your-folder-from-where-you-want-to-run'; 
$request=$myApp->getRequest(); 
if($request instanceof \Zend\Http\PhpEnvironment\Request){ 
    #die($request->getBasePath()); 
    $basePathX=$request->getBasePath(); 
} 
$basePath1=explode('/', $basePathX); 
/* please fix this path as your requirement */ 
array_pop($basePath1); 
$bsPath=implode('/', $basePath1).'/'; 
/* following line will fix your router path, it is important */ 
$myApp->getMvcEvent()->getRouter()->setBaseUrl($bsPath); 
$basePath=new \Zend\View\Helper\BasePath(); 
$basePath->setBasePath($bsPath); 
$view->getHelperPluginManager()->setService('basePath', $basePath); 
$urlHlpr=new \Zend\View\Helper\Url(); 
$urlHlpr->setRouter($myApp->getMvcEvent()->getRouter()); 
$view->getHelperPluginManager()->setService('url', $urlHlpr); 

$resolver=new \Zend\View\Resolver\TemplateMapResolver(); 
$resolver->setMap(array(
     'wmsLayout' => APP_ROOT_PATH . 'module'.DS.'YourModule'.DS.'view'.DS.'layout'.DS.'layout1.phtml', 
     'layout/left-menu' => APP_ROOT_PATH . 'module'.DS.'YourModule'.DS.'view'.DS.'layout'.DS.'left-menu.phtml', 
)); 
$view->setResolver($resolver); 

$viewModel = new \Zend\View\Model\ViewModel(); 
$tmpltFileToLoad=__DIR__ . DS.'your-file-to-render.phtml'; 
$tmpltIdx='_'.md5($tmpltFileToLoad); 
$resolver->add($tmpltIdx, $tmpltFileToLoad); 
$viewModel->setTemplate($tmpltIdx)->setVariables(array(
     'test' => 'shahadat hossain khan' 
)); 
$content=$view->render($viewModel); 

$viewLayout = new \Zend\View\Model\ViewModel(); 
$viewLayout->setTemplate('wmsLayout')->setVariables(array(
     'content' => $content, 
     'anyOtherVariableForLayout'=>'layout variable value', 
    )); 
echo $view->render($viewLayout); 

आशा है कि यह मेरी कोई मदद -

यहाँ पूरा कोड है।

0

sendig ईमेल के लिए मैं निम्नलिखित मॉड्यूल की सिफारिश: https://github.com/WasabiLib/Mail

मॉड्यूल ZF2 संदेश पर आधारित है और यह एक सेवा के रूप में किया गया है। तो आपको केवल servicemanager को कॉल करने की आवश्यकता है।

शरीर विधि ZF2 viewModels को संभालने में सक्षम है।नीचे उदाहरण देखें:

$mail = $this->getServiceLocator()->get("Mail"); 
$viewModel = new ViewModel(array('$yourVariable1' => 'yourContent1', $yourVariable2=>'yourContent2',...)); 
$viewModel->setTemplate("responsive"); 
$mail->setTo('[email protected]'); 
$mail->setBody($viewModel); 
$mail->send(); 

यह एक उत्तरदायी ईमेल टेम्पलेट के साथ बंडल किया गया है जिसे आप अनुकूलित कर सकते हैं। आपको केवल application.config.php पर मॉड्यूल जोड़ने की आवश्यकता है और आप जाने के लिए तैयार हैं।