Skip to content

Variables PHP #136

Closed
erufenix opened this Issue Mar 5, 2013 · 5 comments

2 participants

@erufenix
erufenix commented Mar 5, 2013

Como poner variables PHP e.j
Hola, tengo un problema con las variables PHP

#Mi template

$tbody   = "<div id=\"header\">{{header}}</div>\n".
                "<div id=\"header\">{{title}}</div>\n".
                "<div id=\"content\">{{msg}}</div>\n".
            "<div id=\"footer\">{{footer}}</div>\n";

#mi arreglo

$nomCor = "Erufenix";
$mail->MsgHTML($m->render($tbody, array("header" => "",
                                                            "title"  => $nomCor,
                                                            "msg"    => "Any Text",
                                                            "footer" => "" 
))); 

Si imprimo el resultado del render, en el valor correspondiente a "title" no me despleiga nada, pero los demás si.
La pregunta es ¿Comó poner variables PHP $var en los valores del arreglo?

@bobthecow
Owner

That should work just fine. Anything in the rendering context (the array passed to render) is available when rendering the template. As a minimal test, try this:


$m = new Mustache_Engine;

$tpl = "<div id=\"header\">{{header}}</div>\n" .
         "<div id=\"header\">{{title}}</div>\n" .
         "<div id=\"content\">{{msg}}</div>\n" .
         "<div id=\"footer\">{{footer}}</div>\n";

$title = "Title!";

echo $m->render($tpl, array(
    'header' => 'Header!',
    'title'  => $title,
    'msg'    => 'Msg!',
    'footer' => 'Footer!'
));

The output is:

<div id="header">Header!</div>
<div id="header">Title!</div>
<div id="content">Msg!</div>
<div id="footer">Footer!</div>

Note: I edited your question to add code blocks so I could read it :)

@erufenix
erufenix commented Mar 5, 2013

Your code works if a variable is assigned static value, but if you assign a dynamic value is no longer displayed

$m = new Mustache_Engine;
                    $tpl = "<div id=\"header\">{{header}}</div>\n" .
                    "<div id=\"title\">{{title}}</div>\n" .
                    "<div id=\"content\">{{msg}}</div>\n" .
                    "<div id=\"footer\">{{footer}}</div>\n";
                    $title = "'".$nomCor."'";
                    echo "$title\n";
                    echo $m->render($tpl, array(
                        'header' => 'Header!',
                        'title'  => $title,
                        'msg'    => 'Msg!',
                        'footer' => 'Footer!'
                    ));

#Output

'EDGAR RUBÍ URBINA'
<div id="header">Header!</div>
<div id="title"></div>
<div id="content">Msg!</div>
<div id="footer">Footer!</div>

Sorry for my bad English

@bobthecow
Owner

That worked for me too.

mustache-test

Maybe it's an encoding issue? What happens when you use a $nomCor with no accent characters?

está bien, su inglés es mejor que mi español :)

@erufenix
erufenix commented Mar 6, 2013

Thanks, if the problem were the accents. We solve a utf8_encode ()

@bobthecow
Owner

Hmm. That's still problematic, because we don't want Mustache forgetting to render things just because they're in an unexpected encoding. Do you know what encoding the variable was in before?

@bobthecow bobthecow closed this Apr 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.