简体   繁体   中英

Symfony2 Displaying twig functions from database field

I came to a problem that I want to dinamically display some mandatory data from the user for example when I am loading content from the database.. I dont know how to explain this so I will show the code and tell you what I want to achieve.

So when a user registers he gets a greeting email. In that email I want to display his name and some other information maybe. For now just the name. I know how to access the users name from the database and display it in twig. However, the rendered email is actually saved in the database itself, because I want to change the emails content in the cms.

SO in mysql I have a table Emails and a field called email_content. Inside it I have this:

<tr bgcolor="#FFFFFF">
        <td width="30" height="403" rowspan="2"></td>
        <td width="542" height="100" colspan="3">
            <font style="color:#000000; font-family:Arial, Helvetica, sans-serif; font-size:12px; text-align:left">
                <strong>Welcome {{ user_name }}</strong>, <br>
                <br>
                Thank you for trusting Us.
                <br>
             Lorem ipsum dolor sit amet, consectetuer {{ user_name }} adipiscing elit. Aenean commodo ligula eget dolor.


            </font></td>

        <td width="28" height="100"></td>
    </tr>
    <tr bgcolor="#FFFFFF">
        <td width="394" height="303" colspan="2">


            <font style="color:#000000; font-family:Arial, Helvetica, sans-serif; font-size:11px; text-align:left">


                Lorem ipsum dolor sit amet, {{ user_name }}consectetuer adipiscing elit. Aenean commodo ligula eget dolor. <br>


        <td width="28" height="303"></td>
    </tr>
    <tr bgcolor="#f4f1ec">
        <td width="30" height="160"></td>
        <td width="542" height="160" colspan="3">


        </td>
        <td width="28" height="160"></td>
    </tr>

And in my twig I load the content like this:

{{ skin.emailId.getRegistrationContentEn | raw }}

This is how I send the email itself:

$email = $this->getRepository('ProjectUserBundle:Email')->find(3);
                    $body = $this->container->get('templating')->render('ProjectUserBundle:Email:registration.html.twig', array('user_name' => $user->getFullName()));

                    $em = $this->container->get('doctrine.orm.entity_manager');

                    $skinEmail = $em->getRepository('ProjectSkinBundle:Skin')->getSkinByHost()->getAdminEmailFrom();
//            $skinLogo = $em->getRepository('ProjectSkinBundle:Skin')->getSkinByHost()->getLogo();
//            $logoUrl = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBasePath($skinLogo);

                    $message = Swift_Message::newInstance();
//            $imgUrl = $message->embed(Swift_Image::fromPath($logoUrl));
                    $message->setSubject($this->container->get('translator')->trans('successfull.registration'))
                        ->setFrom($skinEmail, $this->container->getParameter('customer.care.email.sender'))
                        ->setTo($user->getEmail())
                        ->setBody($body, 'text/html');
                    $this->container->get('mailer')->send($message);

Ofcourse this does not load the users name because you cant load twig functions from database. So what can I do to achieve this?

You need not to use raw filter but use include function with template_from_string ( docs here ). This is special extension that you need to enable manually.

{{ include(template_from_string(skin.emailId.getRegistrationContentEn)) }}

To enable this extension you should add service with twig.extension tag and with class parameter as Twig_Extension_StringLoader :

services:
    twig.extension.string_loader:
        class:      Twig_Extension_StringLoader
        tags:
            - { name: twig.extension }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM