Skip to content
fireproofsocks edited this page Dec 9, 2014 · 15 revisions

getPageAssets

This snippet iterates over any assets assigned to the given page (defaults to the current page). It is similar to getResources, except instead of getting resources, it is retrieving assets; the filter parameters therefore apply to the asset parameters and not to page properties.

Parameters

The getPageAssets Snippet accepts the following arguments:

  • &innerTpl (string) formats the Inner Item of List. This can be an inline formatting string or the name of a MODX Chunk (Warning: output filters may require use of a real chunk instead of a formatting chunk). If omitted, the following default value will be used: <li><img src="[[+Asset.url]]" width="[[+Asset.width]]" height="[[+Asset.height]]" alt="[[+Asset.alt]]" /></li>
  • &page_id (integer) Page id of the page whose assets/images you want to see. Defaults to the current page.
  • &outerTpl (string) formats the Outer Wrapper of List (Optional). This can be an inline formatting string or the name of a MODX Chunk.
  • &firstTpl (string) formats the first item of List. This can be an inline formatting string or the name of a MODX Chunk (Warning: output filters may require use of a real chunk instead of a formatting chunk).
  • &lastTpl (string) formats the last Item of List. This can be an inline formatting string or the name of a MODX Chunk (Warning: output filters may require use of a real chunk instead of a formatting chunk).
  • &onOne (string) name of chunk (innerTpl, firstTpl, lastTpl) to use when there is only 1 result (this is rare).
  • &is_active (boolean) Get all active records only. Default: 1.
  • &is_image (boolean) if true, return only images, if false, only other assets. If not set, we get everything. Default will return all assets.
  • &group (string) if supplied, this will filter results to those in the specified group.
  • &limit (integer) Limit the records to be shown (if set to 0, all records will be pulled)
  • &sort (string) which column we should sort the results by (sorting by asset columns requires an "Asset." prefix, sorting by page id is accomplished by PageAsset.page_id). Default: PageAsset.seq -- this will return the assets in the same order you arranged them in the manager. You can specify a random sort order using RAND().
  • &dir (string) which direction should the results be sorted? ASC or DESC. Optional.

Examples

The innerTpl and outerTpl arguments can accept in-line formatting strings or the names of MODX Chunks, but WARNING: the output filters do not work when using a formatting string (sorry, I really tried to make them work). Instead, create a standard chunk and reference it. Often, you will want to restrict your results to images by using the &is_image option.

[[getPageAssets? 
    &innerTpl=`myInnerTpl`
    &is_image=`1`
]]

Where myInnerTpl is

<img 
    src="[[+asset_id:resize=`300x500`]]" 
    width="300" height="500" alt="[[+Asset.alt]]" />

If you don't want or need to enforce resizing, then you can call up the raw Asset URL -- this will be the link to the full-sized original.

<img 
    src="[[+Asset.url]]" 
    width="300" height="500" alt="[[+Asset.alt]]" />

The outerTpl should use the [[+content]] placeholder to indicate where the sum of the innerTpl's will be placed.

[[getPageAssets? 
    &innerTpl=`myInnerTpl`
    &outerTpl=`<div class="myclass">[[+content]]</div>`
]]

For more customizations, you can specify either the &firstTpl or &lastTpl, or both.

[[getPageAssets? 
    &firstTpl=`myFirstTpl`
    &innerTpl=`myInnerTpl`
    &is_image=`1`
]]

Use with getResources

This confuses many users: getResources retrieves resources, not assets. They are two completely different data types living in two completely different database tables. If you are trying to list all the assets associated with a single page, then you can use the previous examples to list the assets for a given page id.

If you want to list all assets for a list of pages, then one way to accomplish this is using two Snippet calls: first use getResources to retrieve the pages you want, then secondly, use getPageAssets to retrieve the assets associated with each of the pages.

<ul>
[[getResources? &tpl=`myPage` &parents=`0`]]
</ul>

Inside the myPage Chunk:

<li>
    <h3>[[+pagetitle]] ([[+id]])</h3>
    Assets: <br/>
    [[!getPageAssets? &page_id=`[[+id]]` &innerTpl=`myAsset`]]
</li>

Now that you're able to retrieve each of your pages using getResources, you will use getPageAssets to iterate over the assets assigned to each page. Format them using the myAsset Chunk, e.g.

<img src="[[+Asset.url]]"/>

No Results

Instead of using yet-another-MODX chunk to display a message when no assets are found, you should instead use a MODX "empty" filter to accomplish this:

[[getPageAssets:empty=`No images found`? &innerTpl=`myAssetChunk`]]

Placeholders

Most of the placeholders here should include "Asset." as a prefix.

  • [[+asset_id]]
  • [[+group]]
  • [[+Asset.url]]
  • [[+Asset.thumbnail_url]]
  • [[+Asset.width]]
  • [[+Asset.height]]
  • [[+Asset.alt]]

Changing the Output Size

The getPageAssets Snippet does not force changes to image sizes directly, instead you can use the resize, scale2h, or scale2w output filters to display different sizes.

E.g. consider the Chunk myInnerTpl:

<li>
    <img src="[[+asset_id:resize=`300x500`]]" 
        width="300" height="500" 
        alt="[[+Asset.alt]]" />
</li>

Called by :

[[getPageAssets? 
    &innerTpl=`myInnerTpl`
]]
Clone this wiki locally