4

So, I have a div that repeats multiple times:

<div class="cmi cm_1_1">
    <span class="cm_img"> 
        <i class="icons">icon_1</i>
    </span> 
    <span class="cm_w">
        something
    </span>                             
    <span class="cm_img"> 
        <i class="icons">icon_2</i>
    </span> 
    <span class="cm_w">
        else
    </span> 
 ... and so on till cm_1_10
</div>

The only thing that will be different will be icon type and wording inside. But the markup is identical.

I was thinking of doing js rendering on this thus using the same markup over and over again (I will simply pass the wording via ajax).

or, should I render the whole thing in server side(php)?

6
  • 2
    Generally, this kind of thing is output via PHP templates. Commented Feb 12, 2016 at 8:43
  • I see. I am confused which is more effective haha. Thanks though. I think I am going to try with js rendering and see if there is any change. Commented Feb 12, 2016 at 8:44
  • 2
    Depends entirely what you're doing. Both will achieve the same effect. It's generally easier in PHP in my opinion, but that's because I work in it more. I believe it may be quicker in JS if you're using AJAX, however I'm not 100% on that. Either way that won't be noticeable. Unless you're manipulating the DOM however I'd suggest PHP for ease-sake. Commented Feb 12, 2016 at 8:46
  • I see. I agree with you. I guess the amount won't be too much. I will try js then php. Thank you for the help! =) Commented Feb 12, 2016 at 8:48
  • 1
    For this task I would prefer PHP. Commented Feb 12, 2016 at 8:56

3 Answers 3

2

You should probably make a PHP function for rendering such blocks, unless you are planing to render that div dynamically after the document loads.

Rendering with JS means being subject to client-side caching, and as such if you display some content and than update it, it may be that the users won't get the last version until they refresh their browser cache. Therefore it's probably not a good idea to render with js.

Here's a previous question of the same kind with a more elaborate answer: PHP vs JavaScript For Dynamic HTML Pages

Sign up to request clarification or add additional context in comments.

1 Comment

The whole idea was to reduce the initial load amount. I think php rendering will make the most sense (as others pointed out) as it won't be loaded till the action is triggered. Thank you for the info! =)
1

In my opinion the most important thing here is to consider if the page you are creating will be static or dynamic. If it is static go for server-side solution. If the content will be changed after page is loaded go for client-side solution (use some js library probably), so that only the changed content will be updated.

1 Comment

Make sense. I decided to go with php for the fact that it won't be loaded till action is triggered. Thanks! =)
1

Templating in JS and PHP has different drawbacks.

JS is faster to load, but can make the DOM hang/lag for a split second if a lot has to be done (hundreds of elements). The advantage is that the HTML snippet can be cached and reused next visit, so that only data has to be loaded.

PHP is slower to load, but if you are able to add GZIP to your PHP output, it will compress the templated and prerendered HTML and the output would load fast, but still load slower than javascript.

So it depends on your usage, but both are valid solutions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.