| <!doctype html> |
| <!-- Generated by FreeMarker/Docgen from DocBook --> |
| <html lang="en" class="page-type-section"> |
| <head prefix="og: http://ogp.me/ns#"> |
| <meta charset="utf-8"> |
| <title>visit, recurse, fallback - FreeMarker Manual</title> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <meta name="format-detection" content="telephone=no"> |
| <meta property="og:site_name" content="FreeMarker Manual"> |
| <meta property="og:title" content="visit, recurse, fallback"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="http://example.com/ref_directive_visit.html"> |
| <link rel="canonical" href="http://example.com/ref_directive_visit.html"> |
| <link rel="icon" href="favicon.png" type="image/png"> |
| <link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338517553"> |
| </head> |
| <body itemscope itemtype="https://schema.org/Code"> |
| <meta itemprop="url" content="http://example.com/"> |
| <meta itemprop="name" content="FreeMarker Manual"> |
| |
| <!--[if lte IE 9]> |
| <div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div> |
| <![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo"> |
| </a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref.html"><span itemprop="name">Reference</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_directives.html"><span itemprop="name">Directive Reference</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_directive_visit.html"><span itemprop="name">visit, recurse, fallback</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li></ul></div></div></div> <div class="main-content site-width"> |
| <div class="content-wrapper no-toc"> |
| <div id="table-of-contents-wrapper" class="col-left"> |
| </div> |
| <div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="ref_directive_attempt.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_specvar.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="ref_directive_visit" itemprop="headline">visit, recurse, fallback</h1> |
| </div></div><div class="page-menu"> |
| <div class="page-menu-title">Page Contents</div> |
| <ul><li><a class="page-menu-link" href="#autoid_122" data-menu-target="autoid_122">Synopsis</a></li><li><a class="page-menu-link" href="#autoid_123" data-menu-target="autoid_123">Description</a><ul><li><a class="page-menu-link" href="#autoid_124" data-menu-target="autoid_124">Visit</a></li><li><a class="page-menu-link" href="#autoid_125" data-menu-target="autoid_125">Recurse</a></li><li><a class="page-menu-link" href="#autoid_126" data-menu-target="autoid_126">Fallback</a></li></ul></li></ul> </div><a name="ref.directive.visit"></a> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_122">Synopsis</h2> |
| |
| |
| |
| <pre class="metaTemplate"><code class="inline-code"><#visit <em class="code-color">node</em> using <em class="code-color">namespace</em>></code> |
| or |
| <code class="inline-code"><#visit <em class="code-color">node</em>></code></pre> |
| |
| |
| |
| <pre class="metaTemplate"><code class="inline-code"><#recurse <em class="code-color">node</em> using <em class="code-color">namespace</em>></code> |
| or |
| <code class="inline-code"><#recurse <em class="code-color">node</em>></code> |
| or |
| <code class="inline-code"><#recurse using <em class="code-color">namespace</em>></code> |
| or |
| <code class="inline-code"><#recurse></code></pre> |
| |
| |
| |
| <pre class="metaTemplate"><code class="inline-code"><#fallback></code></pre> |
| |
| |
| <p>Where:</p> |
| |
| <ul> |
| <li> |
| <code class="inline-code"><em class="code-color">node</em></code>: |
| Expression evaluates to a <a href="xgui_expose_dom.html">node |
| variable</a>. |
| </li> |
| |
| <li> |
| <code class="inline-code"><em class="code-color">namespace</em></code>: A |
| <a href="dgui_misc_namespace.html">namespace</a>, or a |
| sequence of namespaces. A namespace can be given with the |
| namespace hash (a.k.a. gate hash), or with a string literal that |
| store the path of template that could be imported. Instead of |
| namespace hashes, you can use plain hashes as well. |
| </li> |
| </ul> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_123">Description</h2> |
| |
| |
| <p>The <code class="inline-code">visit</code> and <code class="inline-code">recurse</code> |
| directives are used for the recursive processing of trees. In |
| practice, this will mostly be used for <a href="xgui.html">processing XML.</a></p> |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-section3" id="autoid_124">Visit</h3> |
| |
| |
| <p>When you call <code class="inline-code"><#visit |
| <em class="code-color">node</em>></code>, it looks for a |
| user-defined directive (like a macro) to invoke that has the name |
| deducted from the node's name |
| (<code class="inline-code"><em class="code-color">node</em>?node_name</code>) and |
| namespace |
| (<code class="inline-code"><em class="code-color">node</em>?node_namesoace</code>). |
| The rules of name deduction:</p> |
| |
| <ul> |
| <li> |
| <p>If the node doesn't support node namespaces (as text |
| nodes in XML), then the directive name is simply the name of |
| the node |
| (<code class="inline-code"><em class="code-color">node</em>?node_name</code>). |
| <span class="marked-for-programmers">A node does not support node |
| namespaces if the <code class="inline-code">getNodeNamespace</code> method |
| returns <code class="inline-code">null</code>.</span></p> |
| </li> |
| |
| <li> |
| <p>If the node does support node namespaces (as element |
| nodes in XML), then a prefix deduced from the node namespace |
| maybe appended before the node name with a colon used as |
| separator (e.g. <code class="inline-code">e:book</code>). The prefix, and if |
| there is a prefix used at all, depends on what prefixes has |
| been registered with the <code class="inline-code">ns_prefixes</code> |
| parameter of the <code class="inline-code">ftl</code> directive in the <a href="dgui_misc_namespace.html">FTL namespace</a> where |
| <code class="inline-code">visit</code> looks for the handler directive |
| (which is not necessary the same as the FTL namespace where |
| <code class="inline-code">visit</code> was called from, as you will see |
| later). Concretely, if there was no default namespace |
| registered with <code class="inline-code">ns_prefixes</code> then for nodes |
| that does not belong to any namespace (<span class="marked-for-programmers">when <code class="inline-code">getNodeNamespace</code> |
| returns <code class="inline-code">""</code></span>) no prefix is used. If |
| there was a default namespace registered with |
| <code class="inline-code">ns_prefixes</code> then for nodes that does not |
| belong to any namespace prefix <code class="inline-code">N</code> is used, |
| and for nodes that belong to the default node namespace no |
| prefix is used. Otherwise, in both case, the prefix associated |
| to the node namespace with the <code class="inline-code">ns_prefixes</code> |
| is used. If there is not prefix associated to the node |
| namespace of the node, then <code class="inline-code">visit</code> simply |
| behave as if there was no directive found with the proper |
| name.</p> |
| </li> |
| </ul> |
| |
| <p>The node for which the user-defined directive was invoked is |
| available for it as special variable <code class="inline-code">.node</code>. |
| Example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#-- Assume that nodeWithNameX?node_name is "x" --> |
| <#visit nodeWithNameX> |
| Done. |
| <#macro x> |
| Now I'm handling a node that has the name "x". |
| Just to show how to access this node: this node has ${.node?children?size} children. |
| </#macro></pre></div> |
| |
| <p>The output will be something like:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> Now I'm handling a node that has the name "x". |
| Just to show how to access this node: this node has 3 children. |
| Done.</pre></div> |
| |
| <p>If one or more namespaces is specified using the optional |
| <code class="inline-code">using</code> clause, then <code class="inline-code">visit</code> |
| will look for the directives in those namespaces only, with the |
| earlier specified namespaces in the list getting priority. If no |
| <code class="inline-code">using</code> clause is specified, the namespace or |
| sequence of namespaces specified with the <code class="inline-code">using</code> |
| clause of the last uncompleted <code class="inline-code">visit</code> call is |
| reused. If there is no such pending <code class="inline-code">visit</code> call, |
| then the current namespace is used. For example, if you execute |
| this template:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#import "n1.ftl" as n1> |
| <#import "n2.ftl" as n2> |
| |
| <#-- This will call n2.x (because there is no n1.x): --> |
| <#visit nodeWithNameX using [n1, n2]> |
| |
| <#-- This will call the x of the current namespace: --> |
| <#visit nodeWithNameX> |
| |
| <#macro x> |
| Simply x |
| </#macro></pre></div> |
| |
| <p>and this is <code class="inline-code">n1.ftl</code>:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro y> |
| n1.y |
| </#macro></pre></div> |
| |
| <p>and this is <code class="inline-code">n2.ftl</code>:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro x> |
| n2.x |
| <#-- This will call n1.y, becuase it inherits the "using [n1, n2]" from the pending visit call: --> |
| <#visit nodeWithNameY> |
| <#-- This will call n2.y: --> |
| <#visit nodeWithNameY using .namespace> |
| </#macro> |
| |
| <#macro y> |
| n2.y |
| </#macro></pre></div> |
| |
| <p>then this will print:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> |
| n2.x |
| n1.y |
| n2.y |
| |
| Simply x |
| </pre></div> |
| |
| <p>If <code class="inline-code">visit</code> doesn't find a user-defined |
| directive in either FTL namespaces with the name identical to the |
| name deduced with the rules described earlier, then it tries to |
| find an user-defined directive with name |
| <code class="inline-code">@<em class="code-color">node_type</em></code>, or if |
| the node does not support node type property (i.e. |
| <code class="inline-code"><em class="code-color">node</em>?node_type</code> |
| returns undefined variable), then with name |
| <code class="inline-code">@default</code>. For the lookup, it uses the same |
| mechanism as was explained earlier. If it still doesn't find an |
| user-defined directive to handle the node, then |
| <code class="inline-code">visit</code> stops template processing with error. |
| Some XML specific node types have special handling in this regard; |
| see: <a href="xgui_declarative_details.html">XML Processing Guide/Declarative XML Processing/Details</a>. Example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#-- Assume that nodeWithNameX?node_name is "x" --> |
| <#visit nodeWithNameX> |
| |
| <#-- Assume that nodeWithNameY?node_type is "foo" --> |
| <#visit nodeWithNameY> |
| |
| <#macro x> |
| Handling node x |
| </#macro> |
| |
| <#macro @foo> |
| There was no specific handler for node ${node?node_name} |
| </#macro></pre></div> |
| |
| <p>This would print:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output">Handling node x |
| |
| There was no specific handler for node y |
| |
| </pre></div> |
| |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-section3" id="autoid_125">Recurse</h3> |
| |
| |
| <a name="ref.directive.recurse"></a> |
| |
| <p>The <code class="inline-code"><#recurse></code> directive is really |
| syntactic sugar. It visits all children nodes of the node (and not |
| the node itself). So, to write:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#recurse <em>someNode</em> using <em>someLib</em>></pre></div> |
| |
| <p>is equivalent to writing:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#list <em>someNode</em>?children as <em>child</em>><#visit <em>child</em> using <em>someLib</em>></#list></pre></div> |
| |
| <p>However, target node is optional in the |
| <code class="inline-code">recurse</code> directive. If the target node is |
| unspecified, it simply uses the <code class="inline-code">.node</code>. Thus, |
| the terse instruction <code class="inline-code"><#recurse></code> is |
| equivalent to:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#list .node?children as child><#visit child></#list></pre></div> |
| |
| <p>As a side comment for those who are familiar with XSLT, |
| <code class="inline-code"><#recurse></code> is pretty much exactly |
| analogous to the <code class="inline-code"><xsl:apply-templates/></code> |
| instruction in XSLT.</p> |
| |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-section3" id="autoid_126">Fallback</h3> |
| |
| |
| <a name="ref.directive.fallback"></a> |
| |
| <p>As you could learn earlier, in the documentation of the |
| <code class="inline-code">visit</code> directive, the user-defined directive |
| that handles the node is maybe searched in multiple FTL |
| name-spaces. The <code class="inline-code">fallback</code> directive can be used |
| in a user-defined directive that was invoked to handle a node. It |
| directs FreeMarker to continue the searching for the user-defined |
| directive in the further name-spaces (that is, in the name-spaces |
| that are after the name-space of the currently invoked |
| user-defined directive in the list of name-spaces). If a handler |
| for the node is found then it is invoked, otherwise |
| <code class="inline-code">fallback</code> does nothing.</p> |
| |
| <p>A typical usage of this to write customization layer over a |
| handler library, that sometimes passes the handling to the |
| customized library:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#import "/lib/docbook.ftl" as docbook> |
| |
| <#-- |
| We use the docbook library, but we override some handlers |
| in this namespace. |
| --> |
| <#visit document using [.namespace, docbook]> |
| |
| <#-- |
| Override the "programlisting" handler, but only in the case if |
| its "role" attribute is "java" |
| --> |
| <#macro programlisting> |
| <#if .node.@role[0]!"" == "java"> |
| <#-- Do something special here... --> |
| ... |
| <#else> |
| <#-- Just use the original (overidden) handler --> |
| <#fallback> |
| </#if> |
| </#macro></pre></div> |
| |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_directive_attempt.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_specvar.html"><span>Next</span></a></div></div></div></div> </div> |
| </div> |
| <div class="site-footer"><div class="site-width"><div class="footer-bottom"> <p class="last-generated"> |
| Last generated: |
| <time itemprop="dateModified" datetime="2020-07-09T23:48:37Z" title="Thursday, July 9, 2020 11:48:37 PM GMT">2020-07-09 23:48:37 GMT</time> </p> |
| <p class="copyright"> |
| © <span itemprop="copyrightYear">1999</span>–2020 |
| <a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p> |
| </div></div></div></body> |
| </html> |