0

I have this loop data in javascript. And i want to place html css and php for each value.

        var html = '';
              $.each(data, function(name, value) {
                 html += value.email + "<br />" + value.content + "<br />";
              });
              $('#sidebarColumn').html(html);

Here i just listed out some values. Lets say i want to have

<span class="userEmail">value.email<span>
<div class="userContent">value.content</div>
<img src="<?php echo base_url() ?> value.picture" />

How can i place them in JS or there is alternative way to make code readable?

Note* i using .html() here because i want to replace #sidebarColumn previous data and place the loop data in that div

3
  • 1
    If you want readable code and do a lot of this kind of rendering, you should check out a template engine, such as mustache: mustache.github.com Commented Sep 14, 2012 at 15:03
  • 1
    <?php echo base_url() ?> is replaced server-side so will be of no use anyways. +1 for template solution. Commented Sep 14, 2012 at 15:04
  • @HollyStyles i need it because value.picture is a path retrieve from db so it actually just something like this public/images/abc.jpg Commented Sep 14, 2012 at 15:40

2 Answers 2

2

You can write php in <script> tags like this, if that is what you are looking for

<script type="text/javascript">

    var baseUrl = '<?php echo $this->baseUrl() ?>';

    var someVariable = '<?php 
                            //some more php here 
                        ?>'; 
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

try this out

var html = '';
$.each(data, function(name, value) {

html += '<span class="userEmail">'+value.email+'<span>';
html += '<div class="userContent">'+value.content+'</div>';
html += '<img src="<?php echo base_url() ?>"' + value.picture + '/>';
});
$('#sidebarColumn').html(html);

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.