how to use a variable from another .ftl in freemarker - freemarker

I'm trying to use a defined variable in a second template to have the same output in the current one.
template 1:
[#if definition.name=="configMINIMAL_STACK_SIZE"]
[#assign valueMinimalStackSize = definition.value]
[/#if]
Second template:
#define configMINIMAL_STACK_SIZE ((uint16_t)${valueMinimalStackSize})
how could I have the same output of "valueMinimalStackSize " in the second template please ?
Thanks for the help

You could have a template that sets these variables, let's call it "commons.ftl", and then use <#include "commons.ftl"> at the beginning of other templates.

Related

Thymeleaf custom dialect - nested attributes

I would like to ask if it is possible to have nested attributes in HTML tag which can reuse result of previous one. For example
<p custom:one="some text to process" custom:two="process result of custom:one">
where custom:one can be used standalone but custom:two have to be used with custom:one. The final result will be produced by custom:two
if I got you right, you can do it with local variables
You need to specify th:with to declare a variable.
Note that the declared variable is available within the element.
<div th:with="newValue = 'Hello ' + ${val}">
<span th:text="${val}">One</span>
<span th:text="${newValue}>Two</span>
</div>
Let me know if that's what you're looking for.

How to get kendo grid filed value?

I use kendo grid template field. I want to get S_DATA field value.
It is not working.
What is the problem?
field:'S_DATA', title:'CONTENT', width:'20%',
attributes: {style:'text-align:center'},
headerAttributes:{style:'text-align:center'},
template:
"<div class='k-block k-success-colored'>
#if (#=S_DATA#.length >= 100)
{#<span> test </span>#}
else
{#<span>#:S_DATA#</span>#} #
</div>"
Your template is not valid, you need to use the variable S_DATA not the value #=S_DATA# inside the if-expression.
Change
...
#if (#=S_DATA#.length >= 100)
...
to
...
#if (S_DATA.length >= 100)
...
I've create a Dojo showing a working example.
Follow this pattern in templates. I know it will be confusing some times
template : #{JS/KENDO Entities}# <HTML OR TEXT Entities> #{JS/KENDO Entities}#

how to define a variable in laravel blade

How can define a variable in the view side(blade) in laravel?
I found that I can do it in this way:
<?php $var = 'something' ?>
But is there any way to do this like {{ $var = 'something' }} or #var1 = 'something' ?(ofcourse without printing it)
I agree with #Kiril Ivanov answer, but if you still want to do that you can use
#php ($variable = 'test')
Thanks
no, there is no way to define a variable with blade syntax except using the php syntax you have pointed. actually it is not a good practice to define variables in your views and do complex stuff except loops and conditional statements
yes there is a way to do this
first assign your variable like this
{{ $yourvariable='' }}
and after than u can manipulate the variable
#if ($abc['type']=='youresult')
{{ $yourvariable='success'}}
#endif
Hpe this works

Smarty template inheritance only provides local scope variables?

Using the latest version of Smarty 3 with template inheritance I'd like to set a variable in a child template and use it in the parent:
list.tpl
{extends file="page.tpl"}
{block name="head"}
{$page_var = array("abc", "xyz")}
{/block}
{block name="content"}
<div>...</div>
{/block}
page.tpl
<!DOCTYPE html>
<html>
<head>
{block name="head"}
{$page_var = array()}
{/block}
{mytemplatefunc foo=array("bar") + $page_var}
...
The main page template initializes a variable and the child page sets the variable if needed. The main page template would then use that variable when calling a template function. But $page_var is always empty. It's as if the scope of the template variables are always local. Is going back to {include} the only solution?
I've tried {assign scope='global'} and {assign scope='parent'} even though they are intended for includes. I also tried using my own static class methods and variables (to hang onto the value) but it never works. And elsewhere it's been suggested trying to use variables outside of {block} but that goes directly against the documentation.
Why do you redefine $page_var in the Template Extension (list.tpl)?
Have you tried adding values to the original $page_var instead?
So instead of
$page_var = array("abc", "xyz")
you could do
array_push($page_var, "abc", "xyz")

Concatenate smarty variable with foreach

In a .tpl file, I'm using a smarty foreach to concatenate values from an array, separated by pipes "|" :
{foreach from=$attachments item=attachment}{$attachment.file}|{/foreach}
This writes : test1.mp3|test2.mp3|test3.mp3|
Now... I need to pass this result as a variable in an href link.
The problem is I can't include my foreach inside the a href tag.
Also I tried to assign this result to a new smarty variable but can't figure how to do it.
Any help would be great.
Thanks.
You can do it this way:
{assign var=result value=''}
{foreach from=$attachments item=attachment}
{assign var=temp value=$attachment.file}
{assign var=result value=$result$temp|}
{/foreach}

Resources