首页 > 解决方案 > 在 CAKEPHP 中获取 .ctp 文件的 html

问题描述

我已经使用cakephp. 现在我有一些数据要在某些逻辑上呈现。此数据应显示为包含一些输入和其他字段的页面。

一件事是在javascriptside 中执行此操作,但在javascript.

我正在研究的其他解决方案是将数据传递给.ctp文件。获取html并将其传递给我的内容。

我正在尝试这样做

$html = $this->render('myview'); $this->set('html', $view);

但它将 myview 呈现为页面而不是 html 以显示给我的视图。

标签: phpcakephpcakephp-3.0

解决方案


按照代码实现相同,(此代码在控制器的方法/动作中)

        //Variables used in view
        $data = $this->Admins->newEntity();

        // create a builder
        $builder = $this->viewBuilder();

        // configure as needed
        $builder->setLayout('default');
        $builder->setTemplate('/Admins/login');   //Here you can use elements also
        $builder->setHelpers(['Html']);

        // create a view instance
        $view = $builder->build(compact('data'));   //Pass the variables to the view

        // render to a variable
        $output = $view->render();

        //Print output
        pr($output);
        die;

推荐阅读