Get path data from a page other than the calling page?

voltmer1's Avatar

voltmer1

05 Aug, 2016 06:50 PM

I still a little confused about accessing data from a page in Cascade.

I have a page that is supposed to pull in thumbnail images and headlines from news articles (pages) that are actually stored in a directory that is a sibling of the index page for a "News" directory. I have an Folder Index block attached and a format and it seems to pull in the news article data-definition elements, but I need the path for the actual news article page, which is not within the data definition elements.
Is there an easy way to understand how this works?

In my Velocity I am iterating over a collection of pages that I am pulling in structured-data, but I can't of course get the url of each page in the foreach? can I?

  1. Support Staff 1 Posted by Tim on 05 Aug, 2016 08:07 PM

    Tim's Avatar

    Hi,

    Since you mentioned that it is pulling in the Data Definition elements, it sounds like you may already have a render-content-depth setting that is allowing the chooser to render the data that you need.

    The path of the page itself will not be in the data definition content. Instead, it should be included directly under the <system-page> tag. When you have a moment, can you edit your Index Block (the one that you're selecting) and verify that the Indexed Asset Content field has Regular Content checked? This is the setting that forces the Index Block to output the <path> and <link> elements.

    Let me know what you find.

    Thanks

  2. 2 Posted by voltmer1 on 05 Aug, 2016 08:14 PM

    voltmer1's Avatar

    I guess I just don't know how to access that once inside a loop of data-definitions. Here is my index and a snip of the VTL.

    #set ( $news_list_page = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page") )
    #set ( $news_list_page_metadata = $news_list_page.getChildren('dynamic-metadata') )
    
    #set ( $query = $_.query() )
    #set ( $query = $query.byMetadataSet("Main-Taxonomy-List") )
    #set ( $query = $query.byContentType("news-detail") )
    #set ( $query = $query.includePages(true) )
    #set ( $query = $query.includeFiles(false) )
    #set ( $query = $query.includeBlocks(false) )
    #set ( $query = $query.includeFolders(false) )
    #set ( $query = $query.includeSymlinks(false) )
    #set ( $query = $query.sortBy("created") )
    #set ( $query = $query.sortDirection("desc") )
    #set ( $query = $query.maxResults(12) )
    #set ( $news = $query.execute() )
    
    <div class="region news-list-region">
        <div class="region-holder">
            <!-- Filter form -->
            <form class="filter-form">
                <div class="filter-form-holder">
                    <div class="filter-form-item">
                        <span class="filter-form-label">Search by:</span>
                    </div>
                    <div class="filter-form-item">
                        <div class="filter-form-select-wrapper">
                            <select class="filter-form-select" name="" id="select-1">
                                <option value="Recent news">Recent news</option>
                                <option value="2015-2016">2015-16</option>
                                <option value="2014-2015">2014-15</option>
                                <option value="2013-2014">2013-14</option>
                            </select>
                        </div>
                    </div>
                    <div class="filter-form-item">
                        <div class="filter-form-select-wrapper">
                            <label for="topics" class="hide-for-screen-reader">Please choose topic</label>
                            <select class="filter-form-select" name="topics" id="topics">
                                <option value="All Topics">All Topics</option>
                                #foreach( $news_items as $news_list_page_metadata )
                                    #if( 'news-and-events' == $news_items.getChild('name').value )
                                        #set( $taxonomy_list = $news_items.getChildren('value') )
                                        #foreach( $taxonomy_item as $taxonomy_list )
                                            <option value="$taxonomy_item.value">$taxonomy_item.value</option>
                                        #end
                                    #end
                                #end
                            </select>
                        </div>
                    </div>
                    <div class="filter-form-item">
                        <button type="submit" class="filter-form-btn btn btn-primary">Search</button>
                    </div>
                </div>
            </form>
            <!-- News list -->
            <ul class="news-list">
    
                #foreach( $single_news in $news )
                    #set( $headline = $single_news.getStructuredDataNode('headline').textValue )
                    #set( $byline = $single_news.getStructuredDataNode('byline').textValue )
                    #set( $calendar = $single_news.getStructuredDataNode('calendar').textValue )
                    #set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image').textValue )
                    #set( $news_link = $single_news.getChild('link').value )
    
                    #set( $metadata = $single_news.metadata )
                    #set( $dynamic_fields = $metadata.dynamicFields )
    
                    <li class="news-list-item">
                        <article class="news-block is-blue">
                            <div class="news-block-holder">
    
                                #set( $stop_loop = false )
                                #if( $dynamic_fields.size() > 0 )
                                    #foreach( $dynamic_field in $dynamic_fields )
                                        #set( $chanwTextSize = $dynamic_field.Values.size() )
                            
                                        #if( $chanwTextSize > 0 )
                                            #foreach( $value in $dynamic_field.Values )
                                                #if( $value != "" )
                                                    <strong class="news-block-taxonomy">$value</strong>
                                                    #set( $stop_loop = true )
                                                    #break
                                                #end
                                            #end
                                        #end
                                        #if( $stop_loop )
                                            #break
                                        #end
                                    #end
                                #end
                                
                                <div class="news-block-img">
                                    #if( ! $thumbnail_image )
                                        #set( $thumbnail_image = '/assets/images/img-news-list-01.jpg' )
                                    #end
                                    <img src="$thumbnail_image" alt="News Image" /> </div>
                                <div class="news-block-body">
                                    <a href="${news_link}" class="news-block-link">$headline</a>
                                </div>
                            </div>
                        </article>
                    </li>
                #end
            </ul>
    
  3. 3 Posted by Ryan Griffith on 05 Aug, 2016 08:40 PM

    Ryan Griffith's Avatar

    Hi,

    When working with choosers, you are actually given direct access to the chosen asset's API object. So, rather than using .textValue, you would use .asset along with whatever property you wish to access. I would suggest first testing to make sure an asset is selected.

    For example, let's assume you have the image chooser field saved to the variable $image_field:

    #if (!$_PropertyTool.isNull($image_field.asset))
      <img src="${image_field.asset.link}" alt="${image_field.asset.metadata.title}"/>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by voltmer1 on 11 Aug, 2016 01:08 PM

    voltmer1's Avatar

    Ryan,
    I've tried using the .asset.path and it doesn't return anything.

    #set ( $news_link = $single_news.asset.link )
    <div class="news-block-body">
                      <a href="${news_link}" class="news-block-link">$headline</a>
    </div>
    
  5. 5 Posted by Ryan Griffith on 11 Aug, 2016 03:34 PM

    Ryan Griffith's Avatar

    Hi,

    As I mentioned previously, you will need to reference the actual chooser field in order to use .asset.link. For example, the code I provided earlier:

    ## Reference the thumbnail-image chooser field
    #set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image'))
    
    ...
    
    ## If there is a thumbnail chosen, output an IMG tag using the chosen asset's link.
    #if (!$_PropertyTool.isNull($thumbnail_image.asset))
      <img src="${thumbnail_image.asset.link}" alt="${thumbnail_image.asset.metadata.title}"/>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by voltmer1 on 11 Aug, 2016 04:25 PM

    voltmer1's Avatar

    but Ryan, I don't have the chooser field with the page path. I am trying to reference the path to pages that are in a directory of "news-articles", not a field.

  7. 7 Posted by Ryan Griffith on 15 Aug, 2016 12:33 PM

    Ryan Griffith's Avatar

    Ah, my apologies, I thought you needed the link for a chosen asset. In that case, try dropping the .asset from the snippet you provided earlier:

    #set ( $news_link = $single_news.link )
    <div class="news-block-body">
        <a href="${news_link}" class="news-block-link">${headline}</a>
    </div>
    

    Please let me know if you have any questions.

    Thanks!

  8. 8 Posted by voltmer1 on 15 Aug, 2016 04:17 PM

    voltmer1's Avatar

    Thanks Ryan! Worked perfectly. I am also trying to get a thumbnail that is an "asset" assigned within the data definition using your earlier suggestion, with no success. Is this the correct way to do this?

    #set( $thumbnail_image = $single_news.asset('thumbnail-image').link)
    
  9. 9 Posted by voltmer1 on 15 Aug, 2016 07:03 PM

    voltmer1's Avatar

    Sorry Ryan, I didn't see the earlier post (this works):

    #set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image'))
    #if (!$_PropertyTool.isNull($thumbnail_image.asset))
                      <img src="${thumbnail_image.asset.link}" alt="${thumbnail_image.asset.metadata.title}"/>
    

    Thanks again! I guess I don't understand when to use the different methods.

  10. 10 Posted by Ryan Griffith on 15 Aug, 2016 07:49 PM

    Ryan Griffith's Avatar

    Not a problem at all. I am glad to hear the proposed snippet did the trick.

    When in doubt, you can always use the $_PropertyTool.outputProperties() method on any object to see what properties and methods are available to you.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  11. Ryan Griffith closed this discussion on 15 Aug, 2016 07:49 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac