HowTo: use {capture} to avoid using another file for {jrCore_list} calls

  • Overview

    Normally when you use the template="" parameter of a jrCore_list call in a template, that will look for a file in the current skins directory. The technique outlined below shows an alternative method of capturing what would be in that file into a variable so you can skip the need to add an extra file to the file system.

  • Get some data from the database

    A question came up in the forums about how to get something (anything) in from the database.
    The go-to function for that is the {jrCore_list} function. We will look at retrieving the data then 2 separate methods for formatting it in a custom way.
  • The Scenario

    For this example we will get some audio files from the jrAudio modules database.

    In addition to this we will use the URL parameters to limit it to individuals profiles too.

    So at the end of this HowTo we will have a URL called 'junk' that will be able to get a list of the audio files from any profile.

    this url will get all the audio files:
    site.com/junk

    and this url will get all the audio files from someone with the profile_id of 1
    site.com/junk/1
  • We could put a {debug} inside that {capture} to see all the vars that we have available if we wanted to.
  • What we have done above is create a structure that we want the output to be in, then given that structure to the {jrCore_list} function and asked it to output it.

    The result will be what we asked for. A blank page at the url:
    site.com/junk

    Which lists all the audio titles in the database. The output will look similar to this:
    title: song 2
    title: song 1
    title: song 3
    title: song 4
    title: song 6
    title: song 5
    title: song 2
    title: song 3
    title: song 4
    title: song 5

    Because that is all we put in the foreach loop. "output 'Title:{$item.audio_title}' then a line break "
  • Using {capture} as an alternative to a file.

    The first, and most conventional, way to alter the output of the returned data is to use a file and tell the jrCore_list file what that files name is.

    {jrCore_list module="jrAudio" template="some_template.tpl"}

    The above function put into a template would cause the system to look for the template named 'some_template.tpl' in the currently active skins and output the data into it.

    HINT: You can see what data is available by adding {debug} to that template and viewing it.

    The second method however is what we are looking at in this doc, so in this HowTo we are going to use smarty3's {capture} function to simulate having that template so we don't need an extra file.

    Here is the structure:


    {capture assign="template"}
        {literal}
            {foreach $_items as $item}
                title: {$item.audio_title} <br/>
            {/foreach}
        {/literal}
    {/capture}
    
    {jrCore_list module="jrAudio" template=$template}
    
  • When to use a file and when to use {capture}

    Use a file in the file system when you want to it in many locations. Its easier to write template="some_file.tpl" than to copy the whole section of code again into a {capture}.

    Sometimes you will be working on a system where you don't have access to the file system, like via the Template Editor in the ACP. In this case since you can't add a file to the file system a capture is your only option.
  • (optional) Use a variable from the URL

    You may want to alter what is shown on the page by taking a cue from the url, here is an adjustment to the above system that retrieves the item based on the item_id passed in from the URL in the address bar.

    Since our template is called junk.tpl we can see it at the url site.com/junk.

    If we add the number 1 to the end of that URL it becomes available to us in the template on the {$option} or {$_post.option} variables. So lets use it.

    We can understand what variables we have available to work with by adding {debug} into our template.
    {capture assign="template"}
        {literal}
            {foreach $_items as $item}
                title: {$item.audio_title} <br/>
            {/foreach}
        {/literal}
    {/capture}
    
    {jrCore_list module="jrAudio" template=$template profile_id=$option}
  • You can see we have added the code
    profile_id=$option
    into our {jrCore_list} call.

    This makes the full call:
    {jrCore_list module="jrAudio" template=$some_template profile_id=$option}

    Which is the same as:
    {jrCore_list module="jrAudio" template=$some_template profile_id="1"}
    when hard coded.

    So when the visitor visits:
    site.com/junk/1

    They will get all the audio from profile_id=1 's account.

    ----------
    So when the visitor visits:
    site.com/junk/2

    They will get all the audio from profile_id=2 's account.

    ----------
    So when the visitor visits:
    site.com/junk/3

    They will get all the audio from profile_id=3 's account.

    etc.......

Tags