本文整理汇总了PHP中Silex\Application::getTemplateRepository方法的典型用法代码示例。如果您正苦于以下问题:PHP Application::getTemplateRepository方法的具体用法?PHP Application::getTemplateRepository怎么用?PHP Application::getTemplateRepository使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Silex\Application
的用法示例。
在下文中一共展示了Application::getTemplateRepository方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: rootAction
public function rootAction(Application $app, Request $request)
{
$data = $this->prepareInput();
if ($data === null) {
return new JsonResponse(['error' => 'no json data found'], 400);
}
$templateName = isset($data['template']) ? $data['template'] : null;
$templateData = isset($data['data']) ? $data['data'] : null;
if (!$templateName || !$templateData) {
return new JsonResponse(['error' => 'template and data must be set'], 400);
}
$repo = $app->getTemplateRepository();
$template = $repo->getByName($templateName);
if (!$template) {
return new JsonResponse(['error' => "template {$templateName} not found"], 404);
}
$twig = new \Twig_Environment(new \Twig_Loader_String());
$html = $twig->render($template->getTemplate(), $templateData);
$file = new File();
$file->setId(Uuid::uuid4()->toString());
$file->setCreatedAt(date('Y-m-d H:i:s'));
$file->setPath($this->getFilePath($file));
$snappy = new Pdf();
if (substr(php_uname(), 0, 7) == "Windows") {
$snappy->setBinary('vendor\\bin\\wkhtmltopdf.exe.bat');
} else {
$snappy->setBinary('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');
}
$snappy->generateFromHtml($html, $file->getPath());
$repo = $app->getFileRepository();
$repo->add($file);
return new JsonResponse(['id' => $file->getId()], 201);
}
示例2: getTemplateEditForm
private function getTemplateEditForm(Application $app, Request $request, $templateId)
{
$error = $request->query->get('error');
$repo = $app->getTemplateRepository();
$add = false;
$template = $repo->getById($templateId);
if ($template === null) {
$defaults = null;
$add = true;
} else {
$defaults = ['name' => $template->getName(), 'template' => $template->getTemplate(), 'description' => $template->getDescription()];
}
$form = $app['form.factory']->createBuilder('form', $defaults)->add('name', 'text')->add('template', 'textarea')->add('description', 'textarea', array('required' => false))->getForm();
// handle form submission
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
if ($add) {
$template = new Template();
}
$template->setTemplate($data['template'])->setName($data['name'])->setDescription($data['description']);
if ($add) {
if (!$repo->add($template)) {
return $app->redirect($app['url_generator']->generate('templates_add', array('error' => 'Failed adding template')));
}
} else {
$repo->update($template);
}
return $app->redirect($app['url_generator']->generate('templates_index'));
}
return new Response($app['twig']->render('templates/edit.html.twig', ['form' => $form->createView(), 'template' => $template, 'error' => $error]));
}