Using placeholder as variables

Referring to the documentiaon here
I am trying to use this snippet

{% put activeNav = 'home' %}

not with a string but with an array object like this in my pages

{% put pageTitles = {
      '0':  trans("Dashboard") ,
      '1':  trans("Products") ,
      '2':  product.name ,
    }
%}

so far so good, no error.

then inside the layout, I am trying to get the placeholder value like this

{% set breadcrumbs = placeholder('pageTitles') %}

And the page raises an exception

An exception has been thrown during the rendering of a template ("Array to string conversion").

is there another way to pass an object from the page to the layout other then using a placeholder ?

Yes, you’re right; there’s no reason this shouldn’t be appropriate. The error is caused mainly by the assumption that it will store content (a string). You know what they say about assumptions!

A fix has been included here:

1 Like

I just upodated with the latest v.3.7.3

image

and I still have the array to string conversion exception in my theme

here is my code

inside the page:

{%
  put pageTitles = {
    "0":  {
      "title" : trans_me("Dashboard"),
      "link" : "dashboard" | page
    },
    "1":  {
      "title": trans_me("Products"),
      "link": "product/products" | page
    },
    "2":  {
      "title" : product.name,
      "link" : "product/product" | page({'course':product.slug, 'activeTab':'lessons'}),
      },
    "3":  {
      "title" : trans_me("Edit", {"label" :module.name }),
      "link" : "product/product" | page({'course':product.slug, 'activeTab':'lessons'}),
      },
  }
%}

inside the layout

{% set breadcrumbs = placeholder('pageTitles') %}

Can you check that you have the latest october/rain package.

composer show october/rain

the command returns the following:

name     : october/rain
descrip. : October Rain Library
keywords : cms, october, rain
versions : * v3.7.3
type     : library
homepage : http://octobercms.com
source   : [git] https://github.com/octobercms/library.git 21c3571a4832facaf60e0c87ce7c330030305f64
dist     : [zip] https://api.github.com/repos/octobercms/library/zipball/21c3571a4832facaf60e0c87ce7c330030305f64 21c3571a4832facaf60e0c87ce7c330030305f64
path     : /Users/christophevidal/Sites/oc-christophevidal/vendor/october/rain
names    : october/rain

support
source : https://github.com/octobercms/library/tree/v3.7.3

autoload
files
psr-4
October\Rain\ => src/
October\Contracts\ => contracts/
classmap
helpers/

requires
composer/composer ^2.0.0
doctrine/dbal ^2.13.3|^3.1.4
guzzlehttp/guzzle ^7.5
laravel/tinker ~2.0
league/csv ~9.1
linkorb/jsmin-php ~1.0
nesbot/carbon ^2.0
php ^8.0.2
scssphp/scssphp ~1.0
symfony/yaml ^6.0
twig/twig ~3.0
wikimedia/less.php ~4.1

requires (dev)
laravel/framework ^9.0|^10.0
meyfa/phpunit-assert-gd ^2.0.0|^3.0.0
phpbench/phpbench ^1.2
phpunit/phpunit ^8.0|^9.0|^10.0

Ok, interesting. This example below works, but yours doesn’t…

{% put pageTitles = {
      '0':  "Dashboard"|_,
      '1':  "Products"|_,
      '2':  product.name,
    }
%}

{% set breadcrumbs = placeholder('pageTitles') %}

{{ d(breadcrumbs) }}

A patch for this has been included in v3.7.3

1 Like

yes this is working

{%
  put pageTitles = {
    '0':  trans_me("Dashboard"),
    '1':  trans_me("Products"),
    '2':  trans_me("Edit", {"label": product.name} ),
  }
%}

but not this

{%
  put pageTitles = {
    '0':  {
      'title':trans_me("Dashboard"),
      }
    '1':  {
      'title':trans_me("Products"),
      }
    '2':  {
      'title':trans_me("Edit", {"label": product.name} ),
      }
  }
%}

difference being in that there is a second level in the json strcuture (title attribute)

Try it again in v3.7.4

1 Like

it works like a charm ! well done and thanks @daft

1 Like