WEM Community - Forum Thread

>

>

Using WEM Script in custom html script block

Direct link to this thread: forum.wem.io/link?threadid=233

Using WEM Script in custom html script block
2 replies

2020-04-16 18:28
last edited: 2021-02-25 15:44

By: RalphC (WEM)

Moderator

To use WEMScript and WEM Fields in a custom html-script element, you need to use specific hooks <? ... ?>. Combined with a few keywords (see below), this allows for WEM-fields and expressions/statements (if..then..else and choose when..then.. constructions) to be inserted into custom html/css/javascript blocks.

To output values, there are some options for specific situations:

<? ... ?> : indicate a block of WEM-script within a custom script (or widget code)
in between these hooks, WEM-expressions and Fields can be placed.
<?= : output the value standard as a string;
<?js : output the value for safe use in a piece of javascript;
<?html : output the value as html encoded text (escaping html-specific characters);
<?attr : output the value as html-attribute (value as attribute inside an html tag);
<?raw : output the value literally without any formatting change;

To give an example for a block with a calculated background color (and an expression to decide whether to use a span or a div and which field, just to show some combinations):

<? if some expression then ?>
    <div style="background-color: <?attr [CalculatedColorField] ?>;"> 
    <?= [data_field] ?> 
    </div>
<? else ?>
    <span style="background-color: <?attr [CalculatedColorField] ?>;"> 
    <?= [other_data_field] ?> 
    </span>
<? end ?>

Where [CalculatedColorField] is a calculated field with an expression to specify a color-value like:

choose
    when condition 1 then "#f0f0f0"
    when condition 2 then "#ffa500"
    when condition 3 then "yellow"
    when condition 4 then "blue"
    ....
    default "#a0a0a0"
end

But in this case I use a Calculated Field to make the content for the custom script more readable (and the calculation re-usable) ... the whole expression can also directly be placed after the attr.

Ralph - WEM Xpert since 2011

"I speak to machines with the voice of humanity"
-- Marillion, Man of a thousand faces --

Re: Using WEM Script in custom html script block

2023-10-27 11:28
last edited: 2023-10-27 11:44

By: FJP

Update:

Found it:

<? loop [list] ?> 
<? end ?>

Hi there,

Is it possible to create a for-loop in WEM Script to iterate over a (filtered) list in a custom HTML script block? Then for each item I want to assign WEM fields as described in the topic start.

This would give me more control in creating PDF templates when I need to create custom grids/tables with inline styling.

Re: Using WEM Script in custom html script block

2023-10-27 14:57

By: RalphC (WEM)

Moderator

Quoting FJP - 2023-10-27 11:28:

Update:

Found it:

<? loop [list] ?> 
<? end ?>

Hi there,

Is it possible to create a for-loop in WEM Script to iterate over a (filtered) list in a custom HTML script block? Then for each item I want to assign WEM fields as described in the topic start.

This would give me more control in creating PDF templates when I need to create custom grids/tables with inline styling.

Yes, more technical functions and constructs like loop can be found in our documentation for WEMScript for Widgets.

Ralph - WEM Xpert since 2011

"I speak to machines with the voice of humanity"
-- Marillion, Man of a thousand faces --