Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:13:53+00:00 2026-05-25T22:13:53+00:00

Consider the following general issue before I go to the specific problem: You use

  • 0

Consider the following general issue before I go to the specific problem: You use server-side templating (e.g. Smarty) to generate a page in a particular way depending on a particular state. However, you can also change the state and update that page dynamically using Javascript. How do you construct such a system that does not involve replicating one’s work in both Javascript and PHP?

Now, for the specific question that relates to the above. I have a navigation bar for an educational website. Depending on the URL provided by the user, you can be at various levels of navigation: field, subject, course, section, lesson. For example, if the user accesses the following index.php?field=Social_Sciences, the following XML will be returned and parsed by PHP and sent to Smarty such that the appropriate subjects are listed:

<subjects>
    <subject>
    <id>81</id>
        <name>Sociology</name>
        <description>Sociology</description>
        <path>/data/material/Social_Sciences/Sociology/</path>
    </subject>
    <subject>
    <id>82</id>
        <name>Economics</name>
        <description>Economics</description>
        <path>/data/material/Social_Sciences/Economics/</path>
    </subject>
</subject>

Similarly, if a user goes to index.php?field=Social_Sciences&subject=Economics, PHP would parse the following and send it to Smarty:

<courses>
    <course>
        <id>65</id>
        <name>Introduction to Microeconomics</name>
        <description>Introduction to Microeconomics</description>
        <path>
        /data/material/Social_Sciences/EconomicsIntroduction_to_Microeconomics/
        </path>
    </course>
    <course>
        <id>66</id>
        <name>Introduction to Macroeconomics</name>
        <description>Introduction to Macroeconomics</description>
        <path>
        /data/material/Social_Sciences/EconomicsIntroduction_to_Macroeconomics/
        </path>
    </course>
</courses>

Now, the problem is the user can also dynamically navigate using AJAX–that is, they can click through the navigation without reloading the page. That means the navigation XML then has to be parsed by jQuery, which means I have to write the same code to parse the XML twice–once in PHP and once in Javascript. After this system proved unwieldy, I started only using AJAX even on the initial load but this is sub-optimal in terms of speed and number of requests. (If you think this is trivial, there are other examples of such issues in my code.) How can one continue to use PHP templating and dynamic AJAX updating without re-writing the same parsing code twice in both languages?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:13:53+00:00Added an answer on May 25, 2026 at 10:13 pm

    JSON

    If you’re php actions were returning JSON objects, then I would recommend jQuery’s beta product .template()

    In a perfect world, you’re in html 5 and can send the jQuery.template() parser equivalents of your smarty work from the server outright, as found on pandora.com’s html 5 enabled player if you look at the bottom of the page source. it will kind of look like this:

    <!DOCTYPE html>
    
        <html>
        <head>
    <!--- pilfered from jQuery.com's demo linked above --->
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
       $("#aControl").click(function(event){
         $.get("/your/url").success(function(text, response, jQxhr){
            $("#iWillPrintACourseTemplate").tmpl(response).appendTo("#tbodyDestination");
         }).error(function(text, response, jQxhr){
         });
       });
    });
    </script>
    </head>
    <body>
    <script id="iWillPrintACourseTemplate" type="text/x-jquery-tmpl"> 
      <tr class="detail"><td><p>stuff : ${id} - ${}</p><p>${description}</p><p>${path}</p></td></tr>
    </script>
    <div><a id="aControl" href='#'>click me</a></div>
    <table>
    <thead/>
    <tfoot/>
    <tbody id="tbodyDestination">
    </tbody>
    </table>
    </body>
    </html>
    

    not html 5

    then you have to move the jQuery html 5 <script type='text/x-jquery-tmpl'/> declaration into your javascript via a $.template("name", html with templating ${} in it); i would get the html with templating ${} stuff via an ajax call. Why? I would try to keep your html templates server side. to allow easy translation between a SMARTY template and a jQuery.Template() should their syntax ever diverge.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem Consider the following class template <typename T> struct A { T x; };
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Let's consider following, simplified example: We have 2 tabs withing <rich:tabPanel switchType=ajax> , each
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.