How to display a variable inside a bx_repeat?

I set the view vars as following:

        $viewVars = [
            'title'              => 'Listado de rutas',
            'route_url'          => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view',
            'bx_repeat:elements' => $elements,
        ];
echo $this->_oTemplate->parseHtmlByName('home', $viewVars);

The code in the view is following:

        <bx_repeat:elements>
            <tr>
                <td><a href="__route_url__/__id__">__name__</a></td>
                <td>__description__</td>
                <td>__NickName__</td>
            </tr>
        </bx_repeat:elements>

But the __route_url__ is not interpreted as the viewVars value but it's shown as a string. Everything else is replaced, the __id__, __name__, etc.

What am I doing wrong?

Quote · 23 Apr 2014

There is a lot needed for what you want to work, please post full page code so we can make sure everything is there. Check out this exampole:


    function displayToolbarSubmenu($aInfo)
    {
        $aCarts = array();
        foreach($aInfo as $iVendorId => $aVendorCart) {
            //--- Get Items ---//
            $aItems = array();
            foreach($aVendorCart['items'] as $aItem)
                $aItems[] = array(
                    'vendor_id' => $aVendorCart['vendor_id'],
                    'vendor_currency_code' => $aVendorCart['vendor_currency_code'],
                    'item_id' => $aItem['id'],
                    'item_title' => $aItem['title'],
                    'item_url' => $aItem['url'],
                    'item_quantity' => $aItem['quantity'],
                    'item_price' => $aItem['quantity'] * $aItem['price'],
                );

            //--- Get General Info ---//
            $aCarts[] = array(
                'vendor_id' => $aVendorCart['vendor_id'],
                'bx_if:show_link' => array(
                    'condition' => !empty($aVendorCart['vendor_profile_url']),
                    'content' => array(
                        'vendor_username' => $aVendorCart['vendor_profile_name'],
                        'vendor_url' => $aVendorCart['vendor_profile_url'],
                        'vendor_currency_code' => $aVendorCart['vendor_currency_code'],
                        'items_count' => $aVendorCart['items_count'],
                        'items_price' => $aVendorCart['items_price']
                    )
                ),
                'bx_if:show_text' => array(
                    'condition' => empty($aVendorCart['vendor_profile_url']),
                    'content' => array(
                        'vendor_username' => $aVendorCart['vendor_profile_name'],
                        'vendor_currency_code' => $aVendorCart['vendor_currency_code'],
                        'items_count' => $aVendorCart['items_count'],
                        'items_price' => $aVendorCart['items_price']
                    )
                ),
                'vendor_icon' => $aVendorCart['vendor_profile_icon'],
                'bx_repeat:items' => $aItems
            );
        }
        return $this->addCss('toolbar.css', true) . $this->parseHtmlByName('toolbar_submenu.html', array('bx_repeat:carts' => $aCarts));
    }

caredesign.net
Quote · 23 Apr 2014
There is a lot needed for what you want to work, please post full page code so we can make sure everything is there.

The code should be very simple. This is all the code in the action


    /**
     * List all routes
     *
     * @link /m/routes/
     */
    function actionHome ()
    {
        $this->_oTemplate->pageStart();
        $elements = $this->_oDb->getAllRoutes(self::MAX_LIST); // This runs the SQL that fetches the elements in the DB
        $viewVars = [
            'title'              => 'Listado de rutas',
            'route_url'          => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view',
            'bx_repeat:elements' => $elements,
        ];
       
echo $this->_oTemplate->parseHtmlByName('home', $viewVars);
        $this->_oTemplate->pageCode($this->viewVars['title'], true);
    }

I saw that in the module tutorial, the $elements array is modified inside a foreach statement so the route_url is included in the array. Now, it doesn't make sense to me as I should not be required to loop the array to use an external variable outside the bx_repeat block.

Quote · 23 Apr 2014

did you even look at the example I posted? in fact - do a search through your dolphin site for bx_repeat. You will notice a big difference in your structure vs the way it is actually done.

For starters - you have:

$this->_oTemplate->parseHtmlByName('home', $viewVars);

which should be more like:

$this->_oTemplate->parseHtmlByName('home', $viewVars, array('bx_repeat:elements' => $aElements));

 

The way I nderstand it - heres the deal. Your "Elements" is an array, so you need to go through each part so that it is displayed in your end result. Your code is not cycling through the array.

 

 

EDIT I believe something more like this is what you are looking for:

 

$aElements = $this->_oDb->getAllRoutes(self::MAX_LIST);
$aElements = array();
        foreach($aElements as $Element => $aElementsPart) {
           
            $bElements[] = array(
                'title' => 'Listado de rutas',           
                'route_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view',
                'bx_repeat:elements' => $aElements
            );
        }
        return $this->parseHtmlByName('home.html', array('bx_repeat:elements' => $aElements));

 

This is just an example. You will have to tweak to fit your needs exactly.

caredesign.net
Quote · 23 Apr 2014
did you even look at the example I posted? in fact - do a search through your dolphin site for bx_repeat. You will notice a big difference in your structure vs the way it is actually done.

Yes, you posted a lot of code which is not even an action. You didn't highlight what in the code you want me to see. Could you please post a link to what you're saying? I did a search [1], [2], [3] without any success. Maybe I don't know where to search.

For starters - you have:

$this->_oTemplate->parseHtmlByName('home', $viewVars);

which should be more like:

$this->_oTemplate->parseHtmlByName('home', $viewVars, array('bx_repeat:elements' => $aElements));

Following the link I said before, the code in the documentation reads

    function actionHome () {
        $this->_oTemplate->pageStart(); // all the code below will be wrapped by the user design

        $aPosts = $this->_oDb->getAllPosts (getParam('me_blgg_max_posts_to_show')); // get all posts from database
 foreach ($aPosts as $sKey => $aRow) { // add human readable values to the resulted array $aPosts[$sKey]['url'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aRow['id']; $aPosts[$sKey]['added'] = defineTimeInterval($aRow['added']); $aPosts[$sKey]['author'] = getNickName($aRow['author_id']); }     
        $aVars = array ( // define template variables
            'bx_repeat:posts' => $aPosts,
        );
 echo $this->_oTemplate->parseHtmlByName('main', $aVars); // output posts list

        $this->_oTemplate->pageCode(_t('_me_blgg'), true); // output is completed, display all output above data wrapped by user design
 }

Which is the documentation I'm following. Now, in that documentation you can see a foreach() before the template parsing, and that's what I want to avoid.

The way I nderstand it - heres the deal. Your "Elements" is an array, so you need to go through each part so that it is displayed in your end result. Your code is not cycling through the array.

I don't want to cicle through the array to insert the same value in the $elements array as it's not logical for me as the values won't change between each of the array elements.

Quote · 23 Apr 2014

does/can this return more than one result:

$this->_oDb->getAllRoutes(self::MAX_LIST);

 

Also - I said do a search through YOUR dolphin site for bx_repeat, and you will find how it is used. Not search through the boonex forums.

caredesign.net
Quote · 23 Apr 2014

 

I set the view vars as following:

        $viewVars = [
            'title'              => 'Listado de rutas',
            'route_url'          => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view',
            'bx_repeat:elements' => $elements,
        ];
echo $this->_oTemplate->parseHtmlByName('home', $viewVars);

The code in the view is following:

        <bx_repeat:elements>
            <tr>
                <td><a href="__route_url__/__id__">__name__</a></td>
                <td>__description__</td>
                <td>__NickName__</td>
            </tr>
        </bx_repeat:elements>

But the __route_url__ is not interpreted as the viewVars value but it's shown as a string. Everything else is replaced, the __id__, __name__, etc.

What am I doing wrong?

NOTE: from what i see your setting up __route_url__ as part of the $viewVars array. However. The repeates elements are inside the elements array as the $elements array is what your passing to the repeat loop. This __route_vars__ is outside the scope of the repeat loop.

@ProfessorSr. His code is correct, it's just he is using __route_url__ in the wrong spot. It needs to be part of the $elements array to be used within the repeat loop.

https://www.deanbassett.com
Quote · 23 Apr 2014

 

NOTE: from what i see your setting up __route_url__ as part of the $viewVars array. However. The repeates elements are inside the elements array as the $elements array is what your passing to the repeat loop. This __route_vars__ is outside the scope of the repeat loop.

So, there is no way to see any variable INSIDE <bx_repeat:elements> that are OUTSIDE the $elements array?

Quote · 23 Apr 2014

Do you have a URI field in your database table - if so - why not just use that as it is what is used for the url anyways.

caredesign.net
Quote · 23 Apr 2014

 

 

NOTE: from what i see your setting up __route_url__ as part of the $viewVars array. However. The repeates elements are inside the elements array as the $elements array is what your passing to the repeat loop. This __route_vars__ is outside the scope of the repeat loop.

So, there is no way to see any variable INSIDE <bx_repeat:elements> that are OUTSIDE the $elements array?

 
No. To display that repeat section, dolphin loops through the contents of the array being passed to it and displays whats found. If it's not part of that array it will not be displayed.

You need to add that to the function that creates that array if you want it shown in that section.


https://www.deanbassett.com
Quote · 23 Apr 2014

 

 

 

NOTE: from what i see your setting up __route_url__ as part of the $viewVars array. However. The repeates elements are inside the elements array as the $elements array is what your passing to the repeat loop. This __route_vars__ is outside the scope of the repeat loop.

So, there is no way to see any variable INSIDE <bx_repeat:elements> that are OUTSIDE the $elements array?

No. To display that repeat section, dolphin loops through the contents of the array being passed to it and displays whats found. If it's not part of that array it will not be displayed.

You need to add that to the function that creates that array if you want it shown in that section.

Let me see, so, if I have a <bx_repeat> inside a 2nd <bx_repeat> I won't be able to use the variables from the 1st loop inside the 2nd loop?  this doesn't seem to be right.

Is there another way to show the content of an array in a view without using <bx_repeat>?

Quote · 23 Apr 2014

Looks like if there is no easy way to deal with <bx_> templates. I ended using PHP files like this:

    /**
     * List all routes
     *
     * @link /m/routes/
     */
    public function actionHome ()
    {
        $this->_oTemplate->pageStart();
        $elements = $this->_oDb->getAllRoutes(self::MAX_LIST);
        $this->viewVars = [
            'title'              => 'Listado de rutas',
            'route_url'          => $this->_baseHtml . 'view/',
            'bx_repeat:elements' => $elements,
        ];
$this->_baseModule = dirname(dirname(__FILE__)).'/';
        require_once $this->_baseModule . 'templates/base/home.php';
        $this->_oTemplate->pageCode($this->viewVars['title'], true);
    }

And in the home.php file I used standad PHP code

            <? foreach ($this->viewVars['bx_repeat:elements'] as $element) :?>
<tr>
<td><a href="<?=$this->viewVars['route_url'] . $element['id']?>"><?=$element['name']?></a></td>
<td><?=$element['description']?></td>
<td><?=$element['NickName']?></td>
</tr>
<? endforeach ?>

I hope it works for anyone that suffers the same problem.

Quote · 23 Apr 2014

will this work:

 

function actionHome ($aDataEntry)
    {
        $this->_oTemplate->pageStart();
        $elements = $this->_oDb->getAllRoutes(self::MAX_LIST);  // This runs the SQL that fetches the elements in the DB
        $viewVars = [
            'title'              => 'Listado de rutas',
            'route_url'          => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aDataEntry['uri'],
            'bx_repeat:elements' => $elements,
        ];
       
        echo $this->_oTemplate->parseHtmlByName('home', $viewVars);
        $this->_oTemplate->pageCode($this->viewVars['title'], true);
    }

 

 

 

 

on .html page:

 <bx_repeat:elements>
            <tr>
                <td><a href="__route_url__">__name__</a></td>
                <td>__description__</td>
                <td>__NickName__</td>
            </tr>
        </bx_repeat:elements>

caredesign.net
Quote · 23 Apr 2014

 

will this work:

 function actionHome ($aDataEntry)
    {
        $this->_oTemplate->pageStart();
        $elements = $this->_oDb->getAllRoutes(self::MAX_LIST);  // This runs the SQL that fetches the elements in the DB
        $viewVars = [
            'title'              => 'Listado de rutas',
            'route_url'          => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $aDataEntry['uri'],
            'bx_repeat:elements' => $elements,
        ];
       
        echo $this->_oTemplate->parseHtmlByName('home', $viewVars);
        $this->_oTemplate->pageCode($this->viewVars['title'], true);
    }

 on .html page:

 <bx_repeat:elements>
            <tr>
                <td><a href="__route_url__">__name__</a></td>
                <td>__description__</td>
                <td>__NickName__</td>
            </tr>
        </bx_repeat:elements>

Negative, in your code you don't define the $aDataEntry array. The route_url is a prefix for the final link for each of the $elements, so  the REAL url would be

foreach ($elements as $element) {
$realURL[] =BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'view/' . $element['id']
}

BUT I DON'T WANT TO USE A FOREACH IN THE ACTION! It doesn't make any sense to me to loop the $elements to build the URLs and then loop again to show them. As I showed before in my code, I was able NOT to loop the $elements in the action using native PHP code in the views.

 <? foreach ($this->viewVars['bx_repeat:elements'] as $element) :?>
<tr>
<td><a href="<?=$this->viewVars['route_url'] . $element['id']?>"><?=$element['name']?></a></td>
<td><?=$element['description']?></td>
<td><?=$element['NickName']?></td>
</tr>
<? endforeach ?>
Quote · 24 Apr 2014

Just curious - will/can your query return more than one result? And - where exactly are you using this code at - what module?

 

$this->_oDb->getAllRoutes(self::MAX_LIST);

caredesign.net
Quote · 24 Apr 2014

I will try to make this more clear.

A template section like this. <bx_repeat:elements> will only process keys that are inside the array that is passed to it. In this case $elements. It WILL NOT access keys that are outside that array. It is not programmed to do so.

How dolphin processes those is with a simple foreach loop. But unlike a foreach loop you create yourself which you can reference variables that are outside the array your using, the function boonex created to loop that array cannot look at variables outside the array because it is not written to do that.

So i will repeat what i said earlier. If you need that prefix in your repeat section, you must make it part of the array.

If you wrote the code to populate that array then it should be a simple matter to add that prefix to the array.

I'm sure it does not make any sense to you to have a field in the array that is the same for each element in the array. Kinda a waste of space, but thats just the way the repeat function works. All keys used within the repeat must be in the array.

https://www.deanbassett.com
Quote · 24 Apr 2014
Just curious - will/can your query return more than one result? And - where exactly are you using this code at - what module?

$this->_oDb->getAllRoutes(self::MAX_LIST);

It's a custom module created from scratch. And yes, the getALLroutes() returns ALL routes, I thought that would be self explained by the method name

Quote · 24 Apr 2014

So i will repeat what i said earlier. If you need that prefix in your repeat section, you must make it part of the array.

Thanks, you made it very clear, I now understand better how the templates work.

If you wrote the code to populate that array then it should be a simple matter to add that prefix to the array.

I wrote the getAllRoutes() method using the My_Module_Db class, which extends from BxDolModuleDb; using $this->getAll($sql) returns an array that I wouldn't like to loop to add a fiexed value that has nothing to do with the model. Where the link points to is a view (MVC) task, the model couldn't care less.

I'm sure it does not make any sense to you to have a field in the array that is the same for each element in the array. Kinda a waste of space, but thats just the way the repeat function works. All keys used within the repeat must be in the array.

Again, thanks a lot, I still prefer to use PHP in the views as current template engine is too restrictive.

Quote · 24 Apr 2014

Yes. it is to restrictive. Many things that make something easier to do are restrictive.

I have found myself generating my own output with php on many occasions. I used to do it all the time back when i first started writing modules. Now i try to stick to the functions built into dolphin.

Anyhow. using a $this->getAll($sql) database pull to get the fields containing the information we need and then looping that to create a new array of key/value pairs to pass to the template is a procedure all dolphin developers use. Well, most of us anyway. The term (MVC) that you used to me means "Model View Controller" and screams microsoft visual products. May not be what your using, but i and many others here do our coding by hand in a simple text editor.

https://www.deanbassett.com
Quote · 24 Apr 2014
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.