Using WEM Script in custom html script block

Using WEM Script in custom html script block

By: RalphC (WEM)

2020-04-16 12:28
last edited: 2021-02-25 09:44

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 --

Sort by

Re: Using WEM Script in custom html script block

By: FJP

2023-10-27 05:28
last edit: 2023-10-27 05:44

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

By: RalphC (WEM)

2023-10-27 08:57

Moderator

Quoting FJP - 2023-10-27 05: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 --

wemscore 7,150 , 551 views, 2 replies

Direct link to this thread