Search

How do you guys reference params like workspace inside javascript?

http://pastie.org/2101233

The above does not work for me.. I just get the Curly braces output as is.

EDIT

Markdown GUI missing on Chrome for me.

What's quite interesting is that HTML5 doesn't like the XSL comment treatment, hmm..

What's quite interesting is that HTML5 doesn't like the XSL comment treatment, hmm..

how do you mean? i've used both and CDATA in html5 layouts before with a little tweaking. what are you having trouble with?

How do you guys reference params like workspace inside javascript?

i apologize for nobody having replied to this yet. i just now saw your question. the {$param} syntax only works inside of tag attributes, not every quoted string.

in javascript, if you absolutely must reference a parameter, you can call it with a tag (<xsl:value-of select="$param" />). it's going to be pretty unattractive. basically xsl doesn't know/give a damn that you're embedding the tag in javascript. as far as it's concerned, it's all tags and content.

Thanks Fawx, appreciate all your responses and help! I see how this would work using the value-of approach! cheers.

So guys I know this is a pretty old thread... but I'm having some issues with some inline Javascript... and am looking for some experienced heads to help out...

I need to add some HTML block into a json object (which is being used for rendering by a visual graph to provide some information). So far so good. The problem started when I was asked to add some links inside this block.

So my template contains the following

<xsl:template name='user-connections'>
    <xsl:variable name='user-relations'><xsl:call-template name='user-relation-string'/></xsl:variable>
    <xsl:if test='position()!=1'>,</xsl:if>
    {'id':  "<xsl:value-of select='@id'/>",
    'name':  "<xsl:value-of select='concat(@name," (",@id,")")'/>",
    'data':  {relation:&quot;<h4><xsl:value-of select='@name'/>:</h4><xsl:copy-of select='$user-relations'/>&quot;},
    'children':[<xsl:for-each select='connection'><xsl:call-template name='user-connections'/></xsl:for-each>]}
</xsl:template>

<xsl:template name='user-relation-string'>
    <ul>
        <xsl:for-each select='connection'>
            <li>
                <xsl:text>You have Liked or Commented on a Post made by </xsl:text>
            <a href='/user/{@id}'>
                <xsl:value-of select='@name'/>
                </a>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>

The above is called from within a &lt;script> which contains the following block

            var json = {
                'id':  "<xsl:value-of select='user-info/data/id'/>",
                'name':  "<xsl:value-of select='user-info/data/first-name'/><xsl:text> </xsl:text><xsl:value-of select='user-info/data/last-name'/>",
                'data':  {relation:"<h4><xsl:value-of select='user-info/data/first-name'/><xsl:text> </xsl:text><xsl:value-of select='user-info/data/last-name'/>:</h4><xsl:for-each select='user-connections'><xsl:call-template name='user-relation-string'/></xsl:for-each>"},
                'children':[
                    <xsl:for-each select='user-connections/connection'>
                        <xsl:call-template name='user-connections'/>
                    </xsl:for-each>
                ]
            };

Now my problem is that both the json variable and the <a href="samplelink"> use the double quotes making my javascript to invalidate...

Weather I change any to single quotes doesn't matter, if I use cdata for the a href the output < is encoded making it not work... so I was wondering if anyone had any better ideas... or even ways I could do this through an extrnal json file built through symphony xslt I'd be very happy.

I would have a look in the utilities for the JSON utilities (there are two). Maybe they can help you? You would be able to create external files that way, but they may contain a solution to the quotes issue.

I have had it happen before, but not in a Symphony project. It has to do with the quotes needing to be escaped. \" for each quote that turns up in your output.

A method to try is to generate a string instead of an HTML element. That way you can easily escape any quotes that are needed.

Untested: <xsl:value-of select="concat('<a href="/user/', @id, '">', @name, '</a>')"/>

Does that work in place of the a you're creating?

Nope not really... XSLT doesn't allow unescaped < or > tags... and when output they are just placed in the html as encoded characters and thus are output by the browser instead of parsed...

I wonder if I could do this trick in Javascript by parsing all values and doing a replace... not very nice but it works...

string.replace(/&amp;/g, "&").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&quot;/g, '"').replace(/&apos;/g, "'");

Tried the method for escaping the quotes... however because it was done in xslt not in a data-node the value-of was just taking the text eliminating the html structure. Took me so much time trying different things... at least a replace in JS seems to work

Guys,

This seems to do the trick:

<xsl:value-of select="/data/scripts/entry/scripts-top" disable-output-escaping="yes" />

Or if you want to write JS inline in the code, not pulled from data source as I do:

<xsl:text disable-output-escaping="yes">
    <!-- my javascript goes here -->
    alert('works <!>');
</xsl:text>    

Ah right! never came across my mind to use disable-output-escaping on a text node! so simple. Thanks for the tip :)

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details