Eval Smarty Code inside a Smarty Template - smarty

is there a way to evaluate Smarty Code inside an existing Smarty template? For example, I may have the following construct:
smartyTemplate.tpl
<body>
<div id="dynamicPart">
{$valueFromDatabase}
</div>
</body>
Whereas the Smarty variable $valueFromDatabase contains another Smarty Template which I would like to be inserted in place of the variable and then evaluated as a template (with all the logic expressions in replacements neccessary).

without a custom resource, you could have just used an {include file="your/template.tpl"}. Or render the template from the database in code using $smarty->fetch("your/template.tpl") and assigning that to $valueFromDatabase.

{eval var=$valueFromDatabase}
will work

Related

Include blade template without interpreting html

I have a blade template that has some HTML and some JS code. For both case 1 and 2 I'd like to use the same template as it's used in multiple places.
Case 1: include the template with the normal behaviour, so that the code in the template gets executed. This works with the normal behaviour of using #include('template')
Case 2: include the template without the HTML and JS actually being interpreted.
Now I could solve this by making it an x-template component, and then pass a variable to that component that will conditionally wrap the template in <xmp></xmp>.
But the problem is that I use Highlightjs, and that doesn't work if the code is in those xmp tags. It needs to be in <pre><code></code></pre>.
So I'm wondering if you can pass some parameter or do something to include a blade template without actually interpreting the code that's being included.
Update
A bit more clarification. Let's say template.blade.php has this:
<div id="test"></div>
<script>
alert('test');
</script>
In case 1 using #include('template') should alert test when the page is loaded.
In case 2 using #include('template') (but then another way I guess, that's what this question is about) I'd like it to display the code without interpreting it, like would happen when using <code>{{ '<div id="test"></div><script>alert('test');</script>' }}</code>.

How can I use vue variable inside laravel blade's image src?

I am trying to use a vue variable inside blade and image src attribute but vue gives template compiling errors.
Where is what I am trying to do
<img src="uploads/#{{authUser.profilePic}}"/>
I could separate this as a pure template but vue template loads little bit slower than a blade file.
Any help would be greatly appreciated.
Interpolation inside attributes is not available on Vue 2. Instead you use v-bind:src or the shortcut :src to bind a Javascript expression to src.
<img :src="'uploads/' + authUser.profilePic"/>
The javascript expression being string concatenation:
'uploads/' + authUser.profilePic

Implementing the 360 magic spin in smarty

I can't integrate the 360 magic spin in smarty templates. While adding the following code
<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{col}.jpg;">
While adding the attribute of data-magic360-options with filename, the screen will goes blank.
I amusing smarty 2.x.
Smarty (PHP framework) counts all strings {..} like its own directives.
To avoid that, you should use the following code:
{literal}<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{col}.jpg;">{/literal}
or
<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{ldelim}col{rdelim}.jpg;">
Did you forget $ sign for col variable?
data-magic360-options="filename: Bar-360-{col}.jpg;"
Maybe you should use {$col}:
<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{$col}.jpg;">

Changing Laravel Blade Delimiter

I know that you can change the default blade delimiter using
Blade::setEscapedContentTags('[[', ']]');
Blade::setContentTags('[[[', ']]]');
However I don't know where should I put it so that it only affect single blade template as opposed to putting it at app/start/global.php which affect whole application.
If you only want to use different tags for a single view, you can set the tags in the closure or controller action that will generate the view.
Route::get('/', function()
{
Blade::setEscapedContentTags('[[', ']]');
Blade::setContentTags('[[[', ']]]');
return View::make('home');
});
This could be an issue if you want to use the normal tags {{ and }} in an application layout but your custom ones in a nested view - I'm not sure what the best approach there would be.
The solution with Blade::setEscapedContentTags / Blade::setContentTags doesn't work in the latest versions of Laravel (checked at 5.6).
The recommended approach is (https://laravel.com/docs/5.6/blade#blade-and-javascript-frameworks):
Blade & JavaScript Frameworks
Since many JavaScript frameworks also use "curly" braces to indicate a
given expression should be displayed in the browser, you may use the #
symbol to inform the Blade rendering engine an expression should
remain untouched. For example:
Hello, #{{ name }}.
In this example, the #symbol will be removed by
Blade; however, {{ name }} expression will remain untouched by the
Blade engine, allowing it to instead be rendered by your JavaScript
framework.
The #verbatim Directive
If you are displaying JavaScript variables in
a large portion of your template, you may wrap the HTML in the
#verbatim directive so that you do not have to prefix each Blade echo
statement with an # symbol:
#verbatim
<div class="container">
Hello, {{ name }}.
</div>
#endverbatim
Simply use #verbatim directive.wrap your whole code in it and blade will just ignore all the curly braces.

How to fetch() sub-parts of a Smarty template

Background Smarty is a templating engine that separates the presentation layer from the logic layer of web applications. It is well-suited for the Model-View-Control approach to developing web applications. The View can be represented by Smarty templates, which contain only HTML and Smarty tags. The Control can be implemented by PHP files that serve the appropriate views based on the logic contained within them via PHP code. The View is instantiated by displaying the templates via the display() command. Alternatively, a template can be read in as a variable without displaying it via the fetch() command. The file name of the template is the argument to both these commands.
Issue The fetch() command can read an entire template. In order to read parts/sub-parts of a template, each of these parts would normally needed to be stored in a separate file with its own name that can be the argument to the command. This creates needless files.
Question Is it possible to fetch only parts of a Smarty template by somehow marking sections of the template?
Case example Below I present a sample template file with Smarty and HTML tags, as well as the corresponding controller file with PHP code.
Template file (index.tpl)
<html>
<body>
<div id="sec1">
First section
</div>
<div id="sec2">
Second section
</div>
</body>
</html>
Controller file (index.php)
<?php
$smarty = new Smarty;
$template = $smarty->fetch("index.tpl");
?>
In the example above, the $template variable would contain the full output from the template page. Below is a dump of its contents from the example.
$template => string(255)
"<html><body>
<div id="sec1">First section</div>
<div id="sec2">Second section</div>
</body></html>"
However, suppose I wish to read in the code from each of the DIV containers separately, and store them into separate variables, how could I achieve this? For instance, suppose I have a magical function called fetch_sub(). Here's my expectations of using it.
<?php
$smarty = new Smarty;
$div1 = $smarty->fetch_sub("index.tpl", "sec1");
$div2 = $smarty->fetch_sub("index.tpl", "sec2");
?>
Then $div1, etc would contain only the relevant sub-part, instead of the whole template.
Other info I am not a beginner with Smarty and have a fairly good handle on basic concepts, as well as some of Smarty's advanced concepts. Following is my attempts so far at conceptualizing the problem and getting to a solution. My initial rough idea is to demarcate the template into sections using {capture}, and then somehow reference each of these sections. I present an outline example of the idea below.
{capture name=sec1}
<div id="sec1">
First section
</div>
{/capture}
. . .
Smarty (as of Smarty 3.1) has no built-in feature to allow you achieving your goal. I had proposed something similar in 2011, but we haven't come around to implementing it.
Maybe you can have the generated HTML parsed to DOM and help yourself with xpath, or something like that?
You can try this:
sec1.tpl
<div id="sec1">First section</div>
sec2.tpl
<div id="sec2">Second section</div>
index.tpl
<html><body>
{include file="sec1.tpl"}
{include file="sec2.tpl"}
</body></html>
And then You can fetch parts by invoking:
$smarty = new Smarty;
$div1 = $smarty->fetch("sec1.tpl");
$div2 = $smarty->fetch("sec2.tpl");

Resources