Casting Twig_Markup to string in Twig


My HTML, or what I thought was HTML, was actually generating an object of type Twig_Markup rather than a string, and therefore wasn't passing json validation.

Hmm. raw didn't work.

But Twig_Markup has a magic method, __toString() which is called, magically, when Twig_Markup is printed. So, I just had to invoke that directly.

Which was as easy as:

{% set buttonHtml = buttonHtml.__toString(buttonHtml) %)

Happy coding!


About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.