Function load() jQuery + Twig

Asked

Viewed 517 times

3

I’m developing a system using PHP, with the idea of owning only one page. How? (I do not know if it is the best solution, I believe it is not) , I developed a side menu that is fixed and the contents to be changed would be in the center of the page, the side menu contains some "links"ex:

<li class="treeview">
   <a id="menuPerfil">
     <i class="fa fa-user"></i>
   <span>Perfil</span>
    </a>
</li>

By clicking on this "link", I upload a file html which contains a simple form according to the clicked menu link.

I made a script jQuery using the function load():

$("#menuPerfil").click(function () { 
   $('#principalContent').load("formPerfil.html.twig");
});

As links are clicked, a certain link is loaded html. But the html loaded by the load function jQuery cannot access something that is on the index, for example a tag of twig, It’s like carrying a html independent of the index, and not only include it.

I wonder if anyone knows a way to solve using the function .load(), bearing in mind that it does not accumulate the files html loaded on the same page, or some other way to properly load this data, without having to make a different page for each system functionality.

NOTE: I use PHP, twig and jQuery in the project.

2 answers

1

To recover only a div of a content you can use AJAX to request a certain content, then recover only the info you want using the id!

Follow the code below.

$("body").on('click', '#menuPerfil', function () { 

   $.ajax({
     url : "arquivo_com_conteudo_que_quer_pegar.html",
     success : function (retorno) {
           var conteudo =  $('<div>' + retorno + '</div>').find('#ID_DO_CONTEUDO').html();

           $('#principalContent').html(conteudo);
     },
     erro : function (a,b,c) {
           alert('Erro: '+a['status'] + ' ' + c);
     }
   });

   return false;
});
  • Hiago, I tried to use what you suggested and it didn’t work for me, and it inserts the code into html every time I click on a menu (possible to view in Chrome F12). Thanks for your help

  • ah, it’s reloading the page? it’s a link right... missing a false Return in the function . click the id #menuPerfil is already in your DOM or is it added via javascript? I will edit the answer so you can do but one test.

0

It’s not true this case you cited.

As links are clicked, a certain html is loaded. However, the html loaded by the jQuery load function cannot access something that is in the index

When using .load() and upload an HTML file and in that HTML contain javascript codes such as this can yes access any tag of your code. I will demonstrate with an example.

index.php file

    <html>
<head>        
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    
    <style type="text/css"> 
        #menu{ background: red; width: 100px; height: 400px; float: left; }
        #corpo{ background: green; width: 400px; height: 400px; float: left; }        
    </style> 
</head>
<body>        
    <div id='menu'>
        <button id="botao">Me clica</button>
    </div>
    <div id='corpo'></div>    
</body>
    <script>
        $('#botao').click(function(){ $('#corpo').load('teste02.php'); });
    </script>
</html>

File teste02.php

    <div>
    Ola mundo!<br>
</div>
<script>        
    $('#corpo').css('background','white').text('mudei a cor');
    $('#menu').css('background','blue');
</script>
  • Ninja Sneeps, thanks for your help. I make a Twig render in an index.php file of the index.html.Twig page, in this index.php, step an array containing the authenticated user profile, with the 'profile' key. If I go to index, and tag Twig : {perfilUsuario.nome}}, the name that is in the array passed to Twig is displayed, but I try to use {{perfilUsuario.nome}} in this form that I loaded with . load(), it is displayed as common text. And if I give only one include instead of . load(), I can display Twig tags normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.