Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  103] [ 1]  / answers: 1 / hits: 25921  / 14 Years ago, wed, january 5, 2011, 12:00:00

I thought it would be convenient to have reusable code, especially for the navigation bar because it will be the same across all of my pages. This way I won't have to go through each page and manually edit each one individually when a change occurs.



It seems possible to use iframes, but I tried it and the whole page styling went out of whack. I can fix it but I'm wondering if there's something similar.



It would be awesome if something like this could work:




var navbar = document.getElementById('navbar');

navbar.innerHtml = url('navigation.txt');




I'm currently working offline on my site so I don't think I can make xmlhttp requests. Correct? At least I still have yet to get any ajax example to work. This is unfortunate because I think I could easily use this for my application.



Here's my navbar markup. It's not very complicated so I have a feeling I'll just edit it manually in the end.



<nav>
<ul id=navbar>
<li><a href=biosketch.html>Biosketch</a></li>
<li><a href=projects.html>Class Projects</a>
<ul>
<li><a href=projects.html#SeniorProject>Senior Project</a></li>
<li><a href=projects.html#WindTurbine>Wind Turbine</a></li>
</ul>
</li>
<li><a href=#>Resume</a></li>
<li><a href=#>Work Experience</a></li>
<li><a href=#>Contact Me</a></li>
</ul>
</nav>

More From » html

 Answers
42

Like it's been said, typically this is done server side with an include for non AJAX sites. However, I think you can make use of google closure templates. Basically, you define a template in their templating language, that generates a javascript function you can call to render your HTML.



http://code.google.com/closure/templates/docs/helloworld_js.html



Example:



--templates.soy

{namespace templates}

{template .nav}
<ul id=navbar>
<li><a href=biosketch.html>Biosketch</a></li>
<li><a href=projects.html>Class Projects</a>
<ul>
<li><a href=projects.html#SeniorProject>Senior Project</a></li>
<li><a href=projects.html#WindTurbine>Wind Turbine</a></li>
</ul>
</li>
<li><a href=#>Resume</a></li>
<li><a href=#>Work Experience</a></li>
<li><a href=#>Contact Me</a></li>
</ul>
{template}


Then you run the following command to compile it into a javascript function



java -jar SoyToJsSrcCompiler.jar --outputPathFormat templates.js  templates.soy


This will generate a file called templates.js containing a function called templates.nav which you can call from your page like the following:



document.getElementById('navbar').innerHTML = templates.nav();


This is not even using the data merging, which would allow you to pass in a data object to render HTML that is not static. But I've only shown you this since it's all you asked for. I know you could just paste the html into a JS string also, but you's have to deal with the lack of syntax help from your editor.



The one drawback is that this requires JS which you don't seem to mind. However, if you wanted to support JS-less clients, you could generate the template on the server side. There is also a compiler that generates Java google closure methods. You can look for it on their website.



Hope it helps.


[#94361] Tuesday, January 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rickjordond

Total Points: 100
Total Questions: 105
Total Answers: 90

Location: Sweden
Member since Mon, May 8, 2023
1 Year ago
;