Variables - Conditional per Page
Description
Customer would like variables to be emitted only if defined. This requires adding <wwpage:Condition> elements.
Additionally, customer would like for variables to be restricted to "per page" definitions rather than being defined globally for the project group.
Conditions for Variables
Great question Arron!
You make a good point here. We don't provide an easy way to test for the presence of a variable.
However, if you're up for a bit of XSL fun, you can get this working by creating an override to "Transforms\pages.xsl".
By default, page variables are calculated in the replacements code segment as follows:
<!-- Variables --> <!-- --> <xsl:variable name="VarSplitGlobalVariablesAsXML"> <xsl:call-template name="Variables-Globals-Split"> <xsl:with-param name="ParamProjectVariables" select="$GlobalProjectVariables" /> <xsl:with-param name="ParamSplit" select="$ParamSplit" /> </xsl:call-template> </xsl:variable> <xsl:variable name="VarSplitGlobalVariables" select="msxsl:node-set($VarSplitGlobalVariablesAsXML)/wwvars:Variable" /> <xsl:call-template name="Variables-Page-String-Replacements"> <xsl:with-param name="ParamVariables" select="$VarSplitGlobalVariables" /> </xsl:call-template> </xsl:variable>
There are actually two operations going on here:
1. Determine variable values. 2. Format variables as replacements.
You need to know what your variable values are before conditions and replacements are defined. So, we'll start by relocating the "determine variable values" operation to just above the conditions definition.
Search for "<!-- Conditions -->" and move the code to determine variable values there:
<!-- Variables --> <!-- --> <xsl:variable name="VarSplitGlobalVariablesAsXML"> <xsl:call-template name="Variables-Globals-Split"> <xsl:with-param name="ParamProjectVariables" select="$GlobalProjectVariables" /> <xsl:with-param name="ParamSplit" select="$ParamSplit" /> </xsl:call-template> </xsl:variable> <xsl:variable name="VarSplitGlobalVariables" select="msxsl:node-set($VarSplitGlobalVariablesAsXML)/wwvars:Variable" />
Now, inside the conditions block, emit conditions as follows:
<!-- Variables (Conditions) --> <!-- --> <xsl:for-each select="$VarSplitGlobalVariables"> <xsl:variable name="VarVariable" select="." /> <xsl:variable name="VarVariableValue"> <xsl:apply-templates select="$VarVariable" mode="wwmode:variable-string-value" /> </xsl:variable> <xsl:if test="string-length($VarVariableValue) > 0"> <wwpage:Condition name="wwvars:{$VarVariable/@name}" /> </xsl:if> </xsl:for-each>
Finally, make sure your replacements are still being emitted:
<!-- Variables (Replacements) --> <!-- --> <xsl:call-template name="Variables-Page-String-Replacements"> <xsl:with-param name="ParamVariables" select="$VarSplitGlobalVariables" /> </xsl:call-template> </xsl:variable>
That should do it.
Sample 2011.1 project with overrides:
Variables.zip
Variables Scoped per Page
Actually, what is happening is that your variable conditions code is working correctly. The problem is that ePublisher's variables implementation was built to be backwards compatible with certain WebWorks Publisher behaviors. Namely, the fact that once a variable is set, the value remains valid until it is changed.
ePublisher accomplishes this in the template:
<xsl:template name="Variables-Globals-Split">
This template is located in Shared\common\variables\variables.xsl.
So, what you need is a different "strategy" for selecting variable values. To do that, you can create a new variable selection template by cloning "Variables-Globals-Split" to something like "Variables-Per-Page-Only".
The guts would be a cut down version of the original "Variables-Globals-Split", something like:
<xsl:template name="Variables-Per-Split"> <xsl:param name="ParamProjectVariables" /> <xsl:param name="ParamSplit" /> <!-- Access group variables --> <!-- --> <xsl:for-each select="$ParamProjectVariables[1]"> <xsl:variable name="VarVariablesGroup" select="key('wwvars-groups-by-groupid', $ParamSplit/@groupID)[1]" /> <!-- Determine document position --> <!-- --> <xsl:variable name="VarDocumentPosition" select="key('wwvars-documents-by-documentid', $ParamSplit/@documentID)/@position" /> <!-- Emit variables for this page split only --> <!-- --> <xsl:variable name="VarVariablesAsXML"> <!-- Preceding splits and this one --> <!-- --> <xsl:for-each select="$VarVariablesGroup/wwvars:Document[@position = $VarDocumentPosition]"> <xsl:variable name="VarVariablesDocument" select="." /> <xsl:for-each select="$VarVariablesDocument/wwvars:Split[@position = $ParamSplit/@position]"> <xsl:variable name="VarVariablesSplit" select="." /> <!-- Copy all variables --> <!-- --> <xsl:copy-of select="$VarVariablesSplit/wwvars:Variable" /> </xsl:for-each> </xsl:for-each> </xsl:variable> <xsl:variable name="VarVariables" select="msxsl:node-set($VarVariablesAsXML)/wwvars:Variable" /> <!-- Return last unique variable instance --> <!-- --> <xsl:call-template name="Variables-Filter-Last-Unique"> <xsl:with-param name="ParamVariables" select="$VarVariables" /> </xsl:call-template> </xsl:for-each> </xsl:template>
Sample 2011.1 project with overrides:
Variables.zip