tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/11567-how-to-display-a-list-of-blocks-if-they-contain-specific-textCascade CMS: Discussion 2015-03-11T19:22:22Ztag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T17:40:29Z2015-03-03T17:40:29ZHow to display a list of blocks if they contain specific text<div><p>Hi,</p>
<p>I am not sure if this is doable using simple Velocity code. I
believe that even the new query API cannot do what you want. If
this is really the case, then you have two options left: direct
query of the database, or use web services.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T18:24:55Z2015-03-03T18:24:55ZHow to display a list of blocks if they contain specific text<div><p>Okay. Good to know sooner than later.<br>
But for what it's worth, I have the following code which
<em>may</em> be partially relevant to my request (I withheld it
initially to avoid any potential confusion), but obviously, several
things about it aren't relevant (e.g., years). Perhaps it triggers
ideas and sheds light on the realm of possibility?</p>
<pre>
<code>#set ( $years = ["2005", "2006"] )
#foreach ( $year in $years )
#set ( $pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-block[contains(path,'${year}')][system-data-structure[contains(wysiwyg,'${year}')][contains(goals,'${year}')]]") )
<h1>${year}</h1>
<ul>
#foreach ( $page in $pages )
<li>
${_EscapeTool.xml($page.getChild("title").value)}
</li>
#end
</ul>
#end</code>
</pre></div>geofftag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T19:35:47Z2015-03-03T19:35:47ZHow to display a list of blocks if they contain specific text<div><p>I thought you were interested in blocks existing in various
sites, associated with various data definitions. But if you just
want to grab all data definition blocks within a single site, using
the same data definition, then your code should work, assuming of
course that all blocks can be indexed (i.e., not hidden in some
non-indexable folders).</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T19:57:46Z2015-03-03T19:57:46ZHow to display a list of blocks if they contain specific text<div><p>Good, I'm heading down the right path. Now I just need to figure
out which parts of that code I need to replace/modify to address
the criteria in my initial post.</p>
<p>I'll try to remember to post a final, working version when I
figure it out... unless someone else beats me to it... which I'd
absolutely welcome. :)</p></div>geofftag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T20:03:12Z2015-03-03T20:03:12ZHow to display a list of blocks if they contain specific text<div><p>...and just to clarify, you're right that I'm targeting a single
site.</p></div>geofftag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T20:36:55Z2015-03-03T20:36:55ZHow to display a list of blocks if they contain specific text<div><p>Hi,</p>
<p>Depending on the number of Blocks we're talking about, you could
also use the Query API to grab all Block assets and loop over them
to see if they are: a) structured data blocks and b) contain the
field and text you need to search on.</p>
<p>Consider the following as a starting point:</p>
<pre>
<code>#set ( $query = $_.query() )
#set($query = $query.byMetadataSet("Basic"))
#set($query = $query.includePages(false))
#set($query = $query.includeFiles(false))
#set($query = $query.includeBlocks(true))
#set($query = $query.includeFolders(false))
#set($query = $query.includeSymlinks(false))
#set($query = $query.siteName("www.example.com"))
#set ($blocks = $query.execute())
#if ($blocks.size() > 0)
<ul>
#foreach ($block in $blocks)
#set ($ddPath = $block.dataDefinitionPath)
## We only want SD Blocks with a specific Data Definition.
#if (!$_PropertyTool.isNull($ddPath) && $ddPath == "Data-Definition/Path")
#set ($sdField = $block.getStructuredDataNode("external_content"))
## We only want SD Blocks with a specific field that contains a certain value.
#if (!$_PropertyTool.isNull($sdField) && $sdField.contains("product_name"))
<li>${block.name} (${block.parentFolder.path})</li>
#end
#end
#end
</ul>
#end</code>
</pre></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-03T21:57:26Z2015-03-03T21:57:26ZHow to display a list of blocks if they contain specific text<div><p>Thanks for the additional option, Ryan. I'll return after I
learn a thing or two about what I'm doing. ;)</p></div>geofftag:help-archives.hannonhill.com,2010-02-09:Comment/361865012015-03-04T13:54:51Z2015-03-04T13:54:51ZHow to display a list of blocks if they contain specific text<div><p>Not a problem at all, geoff.</p>
<p>The solution you us really depends on your needs. If you would
like to generate this report within Cascade Server, a Format would
be the way to go. To add to that, whether you want to use an Index
Block or the Locator Tool depends on a few factors:</p>
<ul>
<li>Are any of your Blocks set to not be indexable? If so, an Index
Block won't work so well.</li>
<li>Do you have more than 500 Blocks? If so, the Query API will cap
the report at 500.</li>
<li>How often are you planning to run the report? Because Index
Blocks are cached they can sometimes be quicker; however, the size
of the Index Block may negate the caching, making the Locator Tool
a better choice.</li>
</ul>
<p>As always, please don't hesitate to ask questions!</p></div>Ryan Griffith