If I have a handlebar merge variable like {{message}} in my template, how can I have it render HTML output if given the following in PHP:
array(
'name' => 'message',
'content' => '<p>First paragraph.</p><p>Second paragraph.</p>'
)
Right now it outputs the content without parsing the paragraph tags.
If you're using Handlebars I think the proper way to do it is with triple braces, e.g:
{{{html_content}}}
I'm not sure it is ok to mix mc:edit with Handlebars in Mandrill:
Combining Handlebars with either mc:edit regions or merge tags in a single message isn't supported. You should pick Handlebars or mc:edit regions plus merge tags.
https://mandrill.zendesk.com/hc/en-us/articles/205582537-Using-Handlebars-for-dynamic-content
Actually when sending using mandrill the message variable has a field merge_language and when you change to this
'merge_language' => 'handlebars'
It works. For your case i think the value is default 'mailchimp' hence the need to use mc:edit
To answer my own question, I just added mc:edit="message" to the div containing the message, like this:
<div mc:edit="message"></div>
I then added this to my structure:
$template_content = array(
array(
'name' => 'message',
'content' => '<p>First paragraph.</p><p>Second paragraph.</p>'
),
);
Related
I want to send different branded emails. I'm storing the email HTML in the DB because I have an email builder that you just upload an email template made from sendgrid ect and I am passing it to the view blade and escaping all of it. {!!$allhtmlcontent!!}
But then the variables withing the HTML are escaped too and come out like {{$variable}} is there any way to render the blade twice once to pass the HTML with the variables then to pass all the variables in then as well.
I already tried formatting into a string and looking for the variables in the string and pass the whole html string into the blade then.
$emailBlade = CentreEmailTemplate::where('centre_id', $tenant->centre_id)->where('email_template_type_id', 2)->get(); //getting html content
$html = View('emails.Login.LoginDynamic',['html'=>$emailBlade[0]->html_template]); // passing the html content into the blade
$view = $html->render([ // i know this doesnt work :( just demo
'mgrName' => $tenant->name,
'fileUrl' => $fileUrl,
'messageTotal' => $messageTotal,
'isMessageGreater' => $isGreater->message,
'visitorTotal' => $visitorTotal,
'isVisitorGreater' => $isGreater->visitor, //variables that need passed into the html content
'dwellTotal' => $dwellTotal,
'isDwellGreater' => $isGreater->dwell,
'conTotal' => $conTotal,
'isConGreater' => $isGreater->con,
'conRateTotal' => $conRateAvg,
'isConRateGreater' => $isGreater->conRate
]);
just outputs the actual variable name instead of the value.
Thanks in Advance..
One possible solution that I can think of:
$emailBlade = CentreEmailTemplate::where('centre_id', $tenant->centre_id)->where('email_template_type_id', 2)->get()[0]->html_template; //getting html content
$variables = ['{{$mgrName}}' , '{{$fileUrl}}']; //lets say you have two variables
$values = [$tenant->name , $fileUrl];
$email = str_replace($variables , $values , $emailBlade); //now variables are replaced with their values.
Then, in your 'emails.Login.LoginDynami' blade file:
{!! $email !!}
I think what mentioned above is best solution. However as you mentioned that you are already tried this. I can suggest another solution:
Another possible solution is the use of eval(). eval() will evaluate the string as PHP. To use eval() you should first compile the blade string to PHP. which means the {{$variable}} should become something like <?php echo $variable ?>. to do that you can use Blade::compileString($emailBlade). Then you use eval.
However you should be careful about eval. Because you are allowing arbitrary PHP code execution. Therefore if any of the variables are provided by user you should sanitize them.
I want to render a cakephp element which contains static text. The element is supposed to contain help, text, images, ... etc. but it should show only the content relevant to the current page being rendered.
I thought of two ways, one of them is to simply pass html content as a parameter from the view as follows:
echo $this->element('help-bar-content', array(
'title' => 'Element title',
'help_text'=> '<div>
A lot of text and tags go here.
<br/>
<img class="img-class" src="/img/location"/>
</div>'
));
But it is ugly because it would mix html inside php which is inside html. The second way is to have a lot of elements each of them corresponding to a help_text parameter and use them as follows:
echo $this->element('help-bar-content', array(
'title' => 'Element title',
'help_text'=> element('help_element1')));
Which I think is better, But then I would have a LOT of non-reusable elements because I have a lot of views.
Is there a third (better) way to do this?
Put all the help texts in a switch statement or multiple ifs inside the element and give each one a string as identifier and pass that string to the element. Then display the text conditionally based on that string.
<?php if ($helpText === 'foo') : ?>
<p>foo</p>
<?php endif; ?>
Note $helpText follows the convention while $help_text doesnt.
I'm trying to remove every empty element in an input using HTML Purifier, but I want to keep the spans that meet a list of classes. So, for instance:
<div> </div><span class="color-gray"></span><div></div>
Should become:
<span class="color-gray"></span>
The configuration I'm using is:
'AutoFormat.RemoveEmpty.RemoveNbsp' => true,
'AutoFormat.RemoveEmpty' => true,
'CSS.AllowedProperties' => array(),
'Core.RemoveProcessingInstructions' => true,
'HTML.Allowed' => 'a[href|target],h1,h2,p,strong,em,b,i,ul,ol,li,span[class]'
But, as you may imagine, it is not what I expected, as it removes <span>, too. Do you have any idea on how to solve it?
Mm, you won't be able to do this directly, however, it should be a simple patch to RemoveEmpty; just add an extra check for an attribute at the beginning of handleElement.
Here is a template of a possible text file I might need to import into my database:
#NAME:"Test"
#REV:"rev1"
#PRODUCT:"product1","description1","option1"
#PRODUCT:"product2","description2","option1","option2"
"A1","key1","DALI"
"B1","key2",""
"B2","key3","option2"
"C1","key4",""
The first 4 lines is a new addition to the format of these files. I was importing the comma separated data itself successfully before the addition of the comment lines on top.
I was wondering if someone can provide me the most efficient way to put all the values in the comment lines into variables in PHP.
I always have a little trouble when it comes to RegEx. I'm not sure how to best grab the lines starting with a #.
Essentially, I would like to have the following data available to me:
$csv['name']: "Test";
$csv['rev']: "rev1";
$csv['products']: array(
0 => array('name' => "product1", 'desc' => "description1", 'options' => "option1"),
1 => array('name' => "product2", 'desc' => "description2", 'options' => "option1,option2"),
);
$csv['data']: The rest of the data in text file
There could be multiple #PRODUCTS defined, so that is why it would be nice to have an array made from those lines.
Thanks for your help.
Are you using php 5.3? If so, then you can simply read your file using fgets() and detect comments using substr($line, 0, 1). If you don't detect a #,it means it a data line, then pass it on to str_getcsv()...
Cheers
To match something started with #, just use ^ at the beginning of regexp (outside of group)
My current implementation, which is array based stores keys and values in a dictionary, example:
$arr = array(
'message' => 'Paste a flickr URL below.',
);
I realize that it was probably a bad idea storing html inside of a string such as this, but if I'm using gettext then in my .mo/.po files how should I handle storing a similar string? Should I just store words, such as 'Paste a' and 'URL below' and 'flickr' separately?
You should store something like
"Paste a %1 URL below"
and replace all 'vars' using something simple like str_replace('%1', $link, $message);
$link can also be translatable
"%1"
although that might be overkill (does flickr translate between languages?)
rationale behind this is that different languages have different grammatical structures and the ordering of the words wont always be the same.
Update:
as #alex and #chelmertz mention in the comments, try using the sprintf function, which is built for this very thing.
I'd go for this:
$arr = array(
'message' => _('Paste a %s URL below.'),
);
Having all translations as string literals within gettext function calls allows to use standard tools to update *.po catalogues.