Forum

Thread tagged as: Problem

Toggle content via a checkbox problem

Hi,

The setup below allows me to toggle a message, via a checkbox in the page attributes.

I can only get it to work properly if I remove suppress from the perch:pages template, but that outputs the value to the page when the checkbox is ticked.

My setup

toggle-newsletter-banner.html is in pages/attributes

<perch:pages id="toggle_nlb" label="Hide newsletter banner" type="checkbox" value="1" >

It displays a checkbox in the page details tab.

I'm using this code (from this post) to check if the checkbox has been ticked

$toggle = perch_page_attribute('toggle_nlb', [
'template' => 'toggle-newsletter-banner.html'
], true);

if($toggle) {
// do something if checkbox is checked
echo "checkbox is checked";
} else {
// do something if checkbox is unchecked
echo "checkbox is UNCHECKED";
}

This mostly works.

However, because I've not set suppress on perch:pages id="toggle_nlb" the value is output to the page when the checkbox is ticked.

When I add suppress to the template...

<perch:pages id="toggle_nlb" label="Hide newsletter banner" type="checkbox" value="1" suppress >

checkbox is UNCHECKED is output when the checkbox is ticked or not.

Any ideas why?

Perch information

Perch Runway: 3.1.1
Production mode: Production (100)
Installed apps: content (3.1.1), assets (3.1.1), categories (3.1.1), perch_forms (1.11)
DB driver: PDO
DB tables: perch2_backup_plans (1), perch2_backup_resources (1), perch2_backup_runs (5656), perch2_blog_authors (2), perch2_blog_comments (0), perch2_blog_index (69), perch2_blog_posts (3), perch2_blog_posts_to_tags (0), perch2_blog_sections (1), perch2_blog_tags (0), perch2_blog_webmention_queue (0), perch2_blogs (2), perch2_categories (9), perch2_category_counts (0), perch2_category_sets (5), perch2_collection_index (198245), perch2_collection_items (1220), perch2_collection_revisions (190), perch2_collections (6), perch2_content_index (2168), perch2_content_items (198), perch2_content_locks (0), perch2_content_regions (28), perch2_forms (6), perch2_forms_responses (1339), perch2_jw_activity_log_actions (1266), perch2_menu_items (18), perch2_navigation (2), perch2_navigation_pages (4), perch2_page_routes (29), perch2_page_templates (31), perch2_pages (33), perch2_resource_log (19727), perch2_resource_tags (8), perch2_resources (3490), perch2_resources_to_tags (11), perch2_scheduled_tasks (26), perch2_settings (44), perch2_user_passwords (0), perch2_user_privileges (45), perch2_user_role_buckets (0), perch2_user_role_privileges (27), perch2_user_roles (2), perch2_users (8)
Users: 8
App runtimes:
<?php
    $apps_list = array(
        'perch_forms',

    );
Editor plug-ins:
H1: ba167287ed9b818e6008ad2765e5121e
L1: 3fabca5af968c99f35bc9d5a1464a963
F1: 3b606135b33e6a102526838f4152a807
headerColour: #000000
content_singlePageEdit: 1
helpURL:
siteURL: /
hideBranding: 1
content_collapseList: 1
lang: en-gb
update_2.8.13: done
update_runway_2.8.15: done
headerScheme: dark
update_runway_2.8.13: done
latest_version:
on_sale_version:
dashboard: 0
hide_pwd_reset: 1
content_hideNonEditableRegions: 0
content_frontend_edit: 1
logoPath: /perch/resources/logo.png
perch_blog_update: 5.6
perch_blog_post_url: /blog/post.php?s={postSlug}
update_runway_2.8.16: done
update_runway_2.8.17: done
update_runway_2.8.18: done
update_runway_2.8.24: done
update_runway_2.8.25: done
update_runway_2.8.26: done
update_runway_2.8.27: done
perch_blog_site_name:
perch_blog_slug_format: %Y-%m-%d-{postTitle}
perch_blog_akismet_key:
perch_blog_max_spam_days: 0
perch_blog_comment_notify: 0
update_runway_2.8.30: done
jw_activity_log_prune_time: 30
update_runway_2.8.31: done
update_runway_2.8.32: done
update_runway_3.0.8: done
update_runway_3.0.14: done
siteOffline: 0
sidebar_back_link: 0
keyboardShortcuts: 0
content_skip_region_list: 0
assets_restrict_buckets: 0
update_runway_3.1.1: done
PERCH_DEVELOPMENT: 10
PERCH_STAGING: 50
PERCH_PRODUCTION: 100
PERCH_SITEPATH: /Users/stephen/Repositories/project-name
PERCH_DB_USERNAME: root
PERCH_DB_SERVER: localhost
PERCH_DB_DATABASE: russellhomes_
PERCH_DB_PREFIX: perch2_
PERCH_EMAIL_FROM: XXXXXX@XXXXXX
PERCH_EMAIL_FROM_NAME: XXXXXXXXXXX
PERCH_LOGINPATH: /perch
PERCH_PATH: /Users/stephen/Repositories/project-name/perch
PERCH_CORE: /Users/stephen/Repositories/project-name/perch/core
PERCH_RESFILEPATH: /Users/stephen/Repositories/project-name/perch/resources
PERCH_RESPATH: /perch/resources
PERCH_HTML5: 1
PERCH_TZ: Europe/London
PERCH_RWD: 1
PERCH_SCHEDULE_SECRET: gcyR3QmNnakOoT7r
PERCH_CLEAN_RESOURCES:
PERCH_EMAIL_METHOD: smtp
PERCH_EMAIL_HOST: smtp.mailtrap.io
PERCH_EMAIL_SECURE: tls
PERCH_EMAIL_AUTH: 1
PERCH_EMAIL_PORT: 2525
PERCH_EMAIL_USERNAME: 48796d243f6bd3625
PERCH_GMAPS_API_KEY: AIzaSyBVHS4ZG_nD7HP6CuTaUrzIpuPI2EevJwA
PERCH_RUNWAY: 1
PERCH_ERROR_MODE: DIE
PERCH_DATE_LONG: %d %B %Y
PERCH_DATE_SHORT: %d %b %Y
PERCH_TIME_SHORT: %H:%M
PERCH_TIME_LONG: %H:%M:%S
PERCH_RUNWAY_ROUTED:
PERCH_STRONG_PASSWORDS:
PERCH_ASSET_VERSION: 8255e4c4a367cfa3e2bd
PERCH_DEBUG:
PERCH_PREVIEW_ARG: preview
PERCH_TEMPLATE_PATH: /Users/stephen/Repositories/project-name/perch/templates
PERCH_TEMPLATE_FILTERS:
PERCH_DEFAULT_DOC: index.php
PERCH_DEFAULT_EXT: .php
PERCH_PRODUCTION_MODE: 100
PERCH_XHTML_MARKUP:
PERCH_HTML_ENTITIES:
PERCH_SSL:
PERCH_STRIPSLASHES:
PERCH_PROGRESSIVE_FLUSH: 1
PERCH_PARANOID:
PERCH_FORCE_SECURE_COOKIES:
PERCH_DEFAULT_BUCKET: default
PERCH_TRANSLATION_ASSIST:
PERCH_PASSWORD_MIN_LENGTH: 6
PERCH_MAX_FAILED_LOGINS: 10
PERCH_AUTH_LOCKOUT_DURATION: 1 HOUR
PERCH_VERIFY_UPLOADS:
PERCH_PRIV_ASSIST:
PERCH_CUSTOM_EDITOR_CONFIGS:
PERCH_ENABLE_EXIF: 1
PERCH_AUTH_PLUGIN:
PERCH_DB_CHARSET: utf8
PERCH_DB_PORT:
PERCH_DB_SOCKET:
PERCH_APPS_EDITOR_PLUGIN: markitup
PERCH_APPS_EDITOR_MARKUP_LANGUAGE: markdown
Hosting settings
PHP: 7.1.12
Zend: 3.1.0
OS: Darwin
SAPI: cgi-fcgi
Safe mode: not detected
MySQL client: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $
MySQL server: 5.6.38
Free disk space: 48.77 GB
Extensions: Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dom, hash, fileinfo, filter, ftp, gd, SPL, iconv, intl, json, ldap, mbstring, session, standard, mysqlnd, PDO, pdo_mysql, pdo_sqlite, Phar, posix, readline, Reflection, mysqli, SimpleXML, soap, sockets, exif, tokenizer, wddx, xml, xmlreader, xmlrpc, xmlwriter, xsl, zip, cgi-fcgi, imap, gettext, mcrypt, pgsql, pdo_pgsql, igbinary, memcached
GD: Yes
ImageMagick: No
PHP max upload size: 32M
PHP max form post size: 32M
PHP memory limit: 128M
Total max uploadable file size: 32M
Resource folder writeable: Yes
Session timeout: 24 minutes
Native JSON: Yes
Filter functions: Yes
Transliteration functions: Yes
PHP_FCGI_CHILDREN: 4
PWD: /Applications/MAMP/fcgi-bin
PHP_FCGI_MAX_REQUESTS: 200
__CF_USER_TEXT_ENCODING: 0x1F5:0x0:0x2
ORIG_SCRIPT_NAME: /fcgi-bin/php7.1.12.fcgi
ORIG_PATH_TRANSLATED: /Users/stephen/Repositories/project-name/perch/core/settings/diagnostics/index.php
ORIG_PATH_INFO: /perch/core/settings/diagnostics/index.php
ORIG_SCRIPT_FILENAME: /Applications/MAMP/fcgi-bin/php7.1.12.fcgi
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
REQUEST_URI: /perch/core/settings/diagnostics/?extended
QUERY_STRING: extended
REQUEST_METHOD: GET
SERVER_PROTOCOL: HTTP/1.1
GATEWAY_INTERFACE: CGI/1.1
REDIRECT_URL: /perch/core/settings/diagnostics/index.php
REDIRECT_QUERY_STRING: extended
REMOTE_PORT: 58795
SCRIPT_FILENAME: /Users/stephen/Repositories/project-name/perch/core/settings/diagnostics/index.php
SERVER_ADMIN: you@example.com
DOCUMENT_ROOT: /Users/stephen/Repositories/project-name
REMOTE_ADDR: ::1
SERVER_PORT: 80
SERVER_ADDR: ::1
SERVER_NAME: project-name.test
SERVER_SOFTWARE: Apache
PATH: /usr/bin:/bin:/usr/sbin:/sbin
HTTP_COOKIE: _ga=GA1.2.1019966156.1523611376; _gid=GA1.2.1698729128.1526720654; cmsa=1; PHPSESSID=dfc87ebfc012a7e54bab69f6ffed0280
HTTP_ACCEPT_LANGUAGE: en-US,en;q=0.9
HTTP_ACCEPT_ENCODING: gzip, deflate
HTTP_REFERER: https://project-name.test/perch/core/settings/diagnostics/
HTTP_DNT: 1
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
HTTP_USER_AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
HTTP_UPGRADE_INSECURE_REQUESTS: 1
HTTP_CONNECTION: keep-alive
HTTP_HOST: project-name.test
REDIRECT_STATUS: 200
REDIRECT_HANDLER: php-fastcgi
FCGI_ROLE: RESPONDER
PHP_SELF: /perch/core/settings/diagnostics/index.php
REQUEST_TIME_FLOAT: 1526745194.37
REQUEST_TIME: 1526745194
argc: 1
Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I can only get it to work properly if I remove suppress from the perch:pages template, but that outputs the value to the page when the checkbox is ticked.

Do you call perch_page_attributes() somewhere? Perhaps in your <head>? It might be what's outputting your checkbox value.


I think you can use skip-template instead, which enables you to read the field's value regardless of whether you're using suppress:

$attrs = perch_page_attributes(['skip-template' => true]);

if($attrs['toggle_nlb']) {
    // checked
} else {
    // not checked 
}

Hi Hussein,

Thanks for taking the time to respond to this post, really appreciate it.

Yes, perch_page_attributes was being called in the head. I completely forgot about skip-template.

Your solution works great! Many thanks.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Great!

Normally in the <head> I like to specify the template like so:

 perch_page_attributes(['template' => 'seo.html']);

So when you add additional attributes to pages/attributes/default.html they won't be output in the <head>.