Matrix keyword to use value of "declared var" in content


(Evan Wills) #1

Matrix Version: 5.4.3.1

(Note: this is an continuation of Can I access declared vars via keywords)

I would like to be able to access the value of design “declared_vars” via matrix keywords within content divs and or paint layouts.

We have a VERY large (kitchen sink) parse file that uses a lot of SHOW_IF blocks to switch between default content set via customizations and asset specific content set via metadata.

e.g.

<!-- Default content set via design customisation -->
<MySource_AREA id_name="content_via_customisation" design_area="nest_content" print="no">
    <MySource_SET name="type_codes" value="page" />
</MySource_AREA>

<!-- Content specified on a per asset basis via metadata -->
<MySource_AREA id_name="content_via_metadata" design_area="nest_content" print="no">
    <MySource_SET name="type_codes" value="page" />
</MySource_AREA>

<!-- show_if to switch between default and metadata defined content -->
<MySource_AREA id_name="showif_block_1_id" design_area="show_if">
    <MySource_SET name="condition" value="keyword_regexp" />
    <MySource_SET name="condition_keyword" value="asset_metadata_block_1_id" />
    <MySource_SET name="condition_keyword_match" value="^[0-9]+$" />
    <MySource_THEN>
        <!-- START: #content_via_metadata -->
        <MySource_PRINT id_name="content_via_metadata" />
        <!--  END:  #content_via_metadata -->
    </MySource_THEN>
    <MySource_ELSE>
        <!-- START: #content_via_customisation -->
        <MySource_PRINT id_name="content_via_customisation" />
        <!--  END:  #content_via_customisation -->
    </MySource_ELSE>
</MySource_AREA>

The benefit of this approach is that our parse file is very flexible. The disadvantage is that once you have a few customisations and they also get customised making changes to the parse file becomes very difficult because of time out issues when reparsing the parse file.

What I would like to do is use declared_vars to specify asset IDs for the default content then use Matrix Conditional keywords to call the asset specified in the DECLARED_VARS unless there is something specified in metadata.

e.g. in my Design parse file I would have:

<MySource_AREA id_name="cascading_default_ids" design_area="declared_vars" print="no">
	<MySource_DECLARE name="block_1_asset_id" value="" type="text" description="ID of asset to be used as default content for block 1 (can be over-ridden by metadata)" />
</MySource_AREA>

<MySource_AREA id_name="declared_vars_switcher" design_area="nest_content">
    <MySource_SET name="type_codes" value="page" />
</MySource_AREA>

In my metadata I’d have a “related asset” fields called block_1_id which could be used to over-ride the declared var id.

In my paint layout or nested asset, I could use a “declared_vars” keyword inside a conditional keyword block like so:

%begin_asset_metadata_block_1_id%
    <!-- block 1 - override content (per asset) -->
    %asset_metadata_block_1_id^as_asset:asset_contents_raw%
%else_asset_metadata_block_1_id%
    <!-- block 1 - default content (set by parse file or customisation) -->
    %globals_declared_vars_cascading_default_ids_block_1_asset_id^as_asset:asset_contents_raw%
%end_asset_metadata_block_1_id%

With this method, I could remove two out of the three design area blocks need to implement each SHOW_IF block. For my parse file, that translates removing 30 out of 45 design area blocks, which greatly simplifies my parse file and makes it more maintainable.


(Bart Banda) #2

Have you thought about using something like SSJS instead of design areas to achieve this?

For example, you design customisation could potentially just nest in a JS config file, or set a JS config var.

Then in your nested standard pages where you conditionally bring in various content, you could do something like:

<script runat="server">
if(customBlock1){
    <!-- block 1 - override content (per asset) -->
    %globlas_asset_metadata_block_1_id^as_asset:asset_contents_raw%
}else{
    <!-- block 1 - default content (set by parse file or customisation) -->
    %globals_declared_vars_cascading_default_ids_block_1_asset_id^as_asset:asset_contents_raw%
}
</script>

(Evan Wills) #3

Hi Bart

That’s a neat idea.

My problem is that I don’t know how to access declared_vars within nested asset content? Or even if it’s possible.

Using declared_vars is the only way I’ve come up with to not have to have multiple nested_content design area but still get the benefit of cascaded values via design customisations.


(Bart Banda) #4

Well using this method you wouldn’t have to use declared vars at all, and no need to create any more customisations.

With SSJS and global keywords, you can access them in nested content containers as they are all evaluated at the top level together.

So at the start of your design at the top you might have something like:

<script runat="server" src="myGlobalVars.js"></script>

Then inside a standard page that you have nested into the design somewhere you could have that block I posted in my previous example.

Does that make sense?