Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 20, 2014 @ 18:27
    Michaël Vanbrabandt
    0

    Dynamic script tags

    Hello,

    since yesterday I am learning to use umbraco ( version 7 ). Before I used ASP.NET Webforms.

    Now I am trying to do things in umbraco that I frequently use in my previous webforms projects.

    For example:

    I have a master page file which has a ContentPlaceHolder just before the closing body tag to insert script tags comming from the child webpage.

    I have a child page using the masterpage file which has 2 script tags in the ContentPlaceHolder.

    I have another child page using the masterpage file which doesn't contains any script tags.

    How can I do this in umbraco? I have created 2 Templates:

    • Master
    • TextPage

    Content:

    • Contact: TextPage template ( contains the 2 script tags for Google Maps, ... )
    • About us: TextPage template ( contains no script tags )

    On the master template I have added the @RenderBody to render the html content of the contact page.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 20, 2014 @ 18:55
    Dennis Aaen
    0

    Hi Michaël and welcome to our. 

    If I understand your question correct I have a TextPage template, for normally text pages that should contain no script tags, and then you have a contact pages also using the TextPage template, that should contain two javascripts for showing a google map on the page.

    To solve this case I would make a new / specific template for the contact page, so you can add the scripts that you need for this specific page type.It can of course still inherit from your master template.

    In other words: Create, a document type for each of your different page type that you have in your website. It could be news overview page, news item, text page and so on.

    Since you are new to the Umbraco 7. and mybe awhile since you last work with Umbraco I would recommned you to take a look at Umbraco.tv http://umbraco.tv/videos/umbraco-v7/implementor/, these videos are new for the Umbraaco 7. And here a some basic documentation building a website using Umbraco. http://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/

    Hope this helps,

    /Dennis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 20, 2014 @ 19:07
    Michaël Vanbrabandt
    0

    Hello Dennis,

    thanks for the information.

    So if I understand it correctly, the best practise is when you have a page that is in some sort of way a little different then normal pages, you should create a new Document Type and Template?

    Michael

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 20, 2014 @ 19:17
    Dennis Aaen
    0

    Hi Michael,

    Yes, that I what I normally do. So when I have pages that some sort of way a little different then normal page I creates a new Document Type and Template. So if I should  make a news overview I would make a news overview document type and template, and the same for the news item.

    So for the different page types you have in your solution, you make a document type and template.

    If you have pages that use the same markup and have different fields on document type then you can choose to only create a document type, for the page, and on that document type you can set it to use the template that you already have.

    Hope this helps, and make sense.

    /Dennis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 20, 2014 @ 19:30
    Michaël Vanbrabandt
    0

    Hi Dennis,

    I understand it completely but I have only one markup. The @RenderBody() function on the master template inserts the html from the child template. So if I add the script tags at the bottom of my child ( in my example contact page ) template, then they will be added at the bottom of the place where @RenderBody() is defined. But what if there is still content under this @RenderBody() function, like a footer, then the script tags will not be added at the end of the html page before the closing body tag.

    Michael

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 20, 2014 @ 20:09
    Dennis Aaen
    0

    Hi Michael,

    Yeah I know that some people say that scripts should be loaded at the bottom of the page before the closing body tag. Back when I started developing websites, then the scripts should be loaded in the head section.

    I think that you should be good, if you just place the scripts on the templates where it´s needed. So I would just add the google maps scripts on the contact template, and I think that you shold be good. It´s better than just load all the scripts on all your pages.

    If you are good in javascript you could consider take a look at the requireJS http://requirejs.org/, is a JavaScript file and module loader. With this you just require the files that are need to load the page and it only loads the javascript when it needed. Here is an article about RequireJS. http://ajaxian.com/archives/requirejs-asynchronous-javascript-loading and http://www.sitepoint.com/understanding-requirejs-for-effective-javascript-module-loading/

    Hope this helps,

    /Dennis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 21, 2014 @ 15:46
    Michaël Vanbrabandt
    100

    Hi Dennis,

    I have found a solution using the @Section and @RenderSection variables. I have created a new Document Type like you suggested with a new Template where I define the @Section variable.

    After that I added the @RenderSection("NameOfSectionVariable", required: false); on my Master page. Now it adds the scripts of the child pages where @Section NameOfSectionVariable is defined.

    Thanks for the intel about the requirejs, I will also look into that for better site performances.

    Michael

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 21, 2014 @ 16:13
    Dennis Aaen
    0

    Hi Michael,

    Good that you find a solution. :-)

    Glad that I could help you in the right direction. Happy Umbraco coding.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft