2

On various Craft sites, I've needed to provide my clients with access to certain variables used across the site. Things like open hours, specific seasonal color palettes, on / off operation toggles. In the past I have made a Static page that has a single table field with name and value pairs. I have then retrieved these values at the start of my layout template and output them on various pages as needed.

1) Is this this best way to do this?

2) Related question (and more pressing): For a recent project, I have been unable to grab the variables as I've done in the past. Not sure why...

{% set theRows = craft.entries.section('theSection').one() %}
{% set siteVariables = [] %}
{% for row in theRows %}
  {% set siteVariables = siteVariables | merge({ (row.name): row.value}) %}
{% endfor %}

My section entry query is coming up blank and dumping siteVariables causes a server error. The set up seems the same as a working installation. Am I missing something? craft.entries.sectionId(ID).one() also doesn't see to work...

1
  1. Define "best". It's one way of doing it but has a couple of issues. They need to log in to see this (I presume, otherwise one issue is that there are public facing things on the website no actual visitor cares about) but it's not in the dashboard. So there's a log in then navigate to the front end and get the page workflow which is less than ideal) Why not just put them in a global table field and let the client view/copy/edit them there?

  2. Your merge needs square brackets. You're trying to merge an object not an array. Try

{% set theRows = craft.entries.section('theSection').one() %}
{% set siteVariables = [] %}
{% for row in theRows %}
  {% set siteVariables = siteVariables | merge([{ (row.name): row.value}]) %}
{% endfor %}
| improve this answer | |
  • 1
    I hadn't investigated Globals in the past. Thanks so much for the tip! :) – Document Services Mar 30 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.