PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 11, 2022

[FIXED] How to set a html attribute + value via ternary?

 May 11, 2022     symfony, twig     No comments   

Issue

I've tried countless options, but I never get the quotes to wrap around the entire value as soon as there's a space within the value string:

{% set result_string= elements
        ? ( "data-custom-attribute=%s"|format( elements ) )
        : ''
%}

How can you make this work to get for example data-custom-attribute="this is a test" with elements having the value 'this is a test' ?


Solution

The filter format will not add any quotes to the example you've added in your snippet.

You are probably verifying your output in some developer tools which give a false-positive. Verify your output with the generated source (CTRL + U)

Either add the quotes in the variable elements or add them in your html snippet:

{% set elements = "\"foo bar foo\"" %}
{% set result_string= elements
        ? ( "data-custom-attribute=%s"|format( elements ) )
        : ''
%}

{% set elements = "foo bar foo" %}
{% set result_string= elements
        ? ( "data-custom-attribute=\"%s\""|format( elements ) )
        : ''
%}

demo



Answered By - DarkBee
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing