CakePHP: How to use a view element inside of a controller

后端 未结 6 1147
情深已故
情深已故 2020-12-17 20:52

I\'m trying to figure out how to use one of my view elements inside of a controller...

I know, I know: \"Don\'t do that!\" (99% of the time this is

6条回答
  •  甜味超标
    2020-12-17 21:20

    You should use a client-side template. You should never return mark-up from a web service or API, just data. Have your JavaScript take the data, and then format it how you wish.

    For example:

    function getItems() {
        $.get('/some/url', function(response) {
            if (response.data.length > 0) {
                for (var i = 0; i < response.data.length; i++) {
                    var item = response.data[i];
                    $('.results').append('
  • ' + item.title + '
  • '); } } }); };

    This is just an example written off the cuff. Obviously you’ll need to write your own implementation.

提交回复
热议问题