Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:12:35+00:00 2026-06-13T03:12:35+00:00

I am having issues with twig escaping output on method calls to an object

  • 0

I am having issues with twig escaping output on method calls to an object passed into my twig template. I know I can get around this with the |raw filter, but was hoping there might be a way I could simply specify in my object that certain methods are safe and therefore remove the need for the raw filter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:12:37+00:00Added an answer on June 13, 2026 at 3:12 am

    Method calls of objects itself cannot be made html safe because normal objects/entities are not (and should not be) aware of the template engine.

    However, a twig filter or function if aware of the template engine and can be marked html safe in its definition.

    So what you need to do is to implement a html safe twig filter to pass the object to and call the method of your object inside the filter function.

    I guess your templates looks like this:

    <p>{{myObj.getHtmlRepresentation()|raw}}</p>
    

    Now you need to implement a twig filter and change the template to the following:

    <p>{{myObj|html_representation}}</p>
    

    And the twig extension should look like this:

    class MyTwigExtension {
    
        public function getFilters(){
           return array(
            // the magic is the is_safe=>html option
            'html_representation' => new \Twig_Filter_Method($this,'htmlRepresentation',array('is_safe'=>array('html'))),
        }
    
        public function htmlRepresentation($obj){
            return $obj->getHtmlRepresentation();
        }
    
     }
    

    One design consideration: If your object is an entity of a business object of some kind, it sould not create html but you should move the html creation to a template or the twig filter..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Still having issues with this problem. Please help if you can. So I am
I am having issues while inserting XML into XML column in table using SQL
Having issues calling the ReportingService2005.FindItems() method from within powershell v2. $rs = New-WebServiceProxy -Uri
Im having issues trying to get a custom snapin to display in the Add/Remove
Having issues trying to select html injected into the DOM using jquery's load. Works
I'm having issues creating an ActionLink using Preview 5. All the docs I can
I am having issues using twig templates for email in SF2. First, I created
I'm having issues upgrading a pom file from Hibernate 3.3.2 to 3.6.0. I can
Having issues trying to get this jquery code to work. The idea is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.