Add Custom Js and Css Files within Data Definitions

Holsten, Adam's Avatar

Holsten, Adam

19 Jun, 2014 07:14 PM

I’ve been trying to add usable fields within my site’s data definitions to allow the user to add custom .css and .js files. I’m not exactly sure how to do this and I was hoping you might be able to help.

I looked through our site’s existing formats and came across this code below that looks like it would work.

#set ($pageData = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure"))
#set ($pageType = $_XPathTool.selectSingleNode($pageData, "@definition-path"))
#if ($pageType.value == "Program Interior Page")
    #set ($pageData = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/system-page[name='index' and not(@reference)]/system-data-structure"))
#end

#set ($javascriptFiles = $_XPathTool.selectNodes($pageData, "file[path != '/']/link"))

#if($javascriptFiles.size() > 0)
    #foreach($js in $javascriptFiles)
        <script src="${js.value}" type="text/javascript"></script>
    #end

   #else

                <script src="https://philau.edu/business/inc/js/business.js" type="text/javascript"></script>

#end

I am wondering how I could create a similar format to upload css files. Any information would be appreciated.
-Thanks

  1. 1 Posted by Ryan Griffith on 20 Jun, 2014 11:14 AM

    Ryan Griffith's Avatar

    Hi Adam,

    You should be able to simply copy the Format you provided and replace the <script> tag with a <link> tag and associated attributes. Everything else should remain the same.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Charlie Holder on 20 Jun, 2014 07:14 PM

    Charlie Holder's Avatar

    Hey Adam --

    FWIW, you can do some if type checking with an XPath expression. It may come in handy to you in the future. An example would be if I wrote your code this way:

    #set ( $sib = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block") )
    #set ( $callingPage = $_XPathTool.selectSingleNode($sib, "calling-page") )
    #if ( $_XPathTool.selectSingleNode($callingPage, "system-page/system-data-structure[@definition-path='Program Interior Page']") )
        #set ( $pageData = $_XPathTool.selectSingleNode($sib, "system-page[name='index' and not(@reference)]/system-data-structure") )
    #else
        #set ( $pageData = $_XPathTool.selectSingleNode($callingPage, "system-page/system-data-structure") )
    #end
    #set ( $javascriptFiles = $_XPathTool.selectNodes($pageData, "file[path!='/']/link") )
    #if ( $javascriptFiles.size() > 0 )
        #foreach ( $js in $javascriptFiles )
            <script src="${js.value}" type="text/javascript"></script>
        #end
    #else
        <script src="https://philau.edu/business/inc/js/business.js" type="text/javascript"></script>
    #end
    

    It's roughly the same length as the code you posted, it just does the if statement on the Data Definition name in another way.

    Edit: I'm not sure why I switched the <script> tags to <link> tags. I switched them back.

  3. 3 Posted by Holsten, Adam on 20 Jun, 2014 07:44 PM

    Holsten, Adam's Avatar

    Thanks Charlie,
    I'll use this in the future. I was able to get the format working by following Ryan's recommendation.
    ________________________________

  4. 4 Posted by Ryan Griffith on 20 Jun, 2014 07:57 PM

    Ryan Griffith's Avatar

    Thank you for following up, Adam. I am glad to hear you were able to get your Format working.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  5. Ryan Griffith closed this discussion on 20 Jun, 2014 07:57 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac