Custom instance of Zend View and access to View Helpers - zend-framework2

I use Zend View to render HTML code to generate PDF file. My code looks like:
$viewModel = new ViewModel(array(
'order' => $order
));
$viewModel->setTemplate('invoice');
$resolver = new TemplatePathStack();
$resolver->addPath(realpath(__DIR__ . '/../view/app'));
$renderer = new PhpRenderer();
$renderer->setResolver($resolver);
$html = $renderer->render($viewModel);
And here I've got html code that I can pass to mPDF class to generate PDF document.
Anyway, I want to use View Helper in "invoice" template to do some stuff. If I define my custom helper in module.php file, it isn't accessible. So I need to define it in some another way, but I can't find how... Could you guys helper me?
Big thanks!

You can attach view helpers with name and reference to class name.
see https://framework.zend.com/manual/2.4/en/modules/zend.view.helpers.advanced-usage.html
Here is an example for you, expecting you are using a invokable plugin class.
...
$renderer = new PhpRenderer();
$renderer->setResolver($resolver);
$renderer->getHelperPluginManager()->setInvokableClass('viewhelpername', 'Path/To/Class');

Related

How to Fix "Attempted to call an undefined method named "createNamedBuilder..."?

I'm trying to create a named form (builder) within my controller like
...
$form = $this->createNamedBuilder('form1', $data)
->add(...)
->getForm();
But i get the title-mentioned error.
When i check the abstract controller trait class there are no createNamed() or createNamedBuilder() functions in there.
How do i create a named form with a form builder?
Kind Regards
According to This you need to acquire the FormFactory(Interface via Dependency Injection).
Adding FormFactoryInterface $formFactory to my controller method's parameters and using it like
$formFactory->createNamedBuilder('name', FormType::class, $data)...
did the trick.

ServiceStack Razor - Html.RenderAction equivalent

I have the requirement to use Html.RenderAction like you would in ASP.NET MVC.
For instance I have a Home Page with News and Products on.
I would like to do for instance
#Html.RenderAction("/api/products/featured")
Which would start a new service call and output the template to the html stream.
Is this possible using ServiceStack Razor and if so how do I accomplish it?
The PartialExamples.cshtml test page shows different examples of rendering a razor view inside a page, e.g:
Using the new RenderToAction() method which lets you execute a Service and it's rendered partial view with a route and QueryString, e.g:
#Html.RenderAction("/products/1")
This also takes an optional view name if you want a different view than the default:
#Html.RenderAction("/products/1", "CustomProductView")
There's also the normal Html.Partial() to specify which view and model you want to render in the page, e.g:
#Html.Partial("GetProduct",
base.ExecuteService<ProductService>(s => s.Any(new GetProduct { Id = 1 })))
ExecuteService is simply a wrapper around the equivalent ResolveService in a using statement, i.e:
#{
Response response = null;
using (var service = base.ResolveService<ProductService>())
{
response = service.Any(new GetProduct { Id = 1 });
}
}
#Html.Partial("GetProduct", response)
The new RenderToAction() method in Razor Views was added in v4.0.34+ which is now available on MyGet.
*I may be duplicating my answer or I lost it somehow
Looking at the ServiceStack.Razor.ViewPage class there is an Html property of type ServiceStack.Html.HtmlHelper. I don't see 'RenderAction' as a method (or extension method) on this class so it doesn't appear to be available. There is a 'Partial' method that takes the ViewName and an overload that takes a ViewName and an object. Based on your above comment this doesn't appear to be a useful solution.
If I'm correct about the above, I think you'd need your 'Featured View Template' to pull in the data. Could add soemthing like
{ FeaturedResponse products = new JsonServiceClient("http://localhost").Get<FeaturedResponse>("/api/products/featured"); }
to your template. This would allow you to use the products variable like a Model.
Or, use JavaScript to pull the data into the template. You would have have to use JavaScript to get your data into the HTML elements, though.
You could then render the template using #Html.Partial('Featured')
Hope this helps.

Umbraco.getDictionaryItem Helper in Custom Controller

Can i access #Umbraco.getDictionaryItem Helper in my Custom Controller in my custom Area and then add it to the model of the custom partial page which i am rendering through plain jquery Ajax.
And also it would b great if i can access it in my Surface Controller plugin.
Thanks,
Sher
You should be able to access it in your server side files, yes. You just need to make sure you have a reference to the correct Umbraco DLLs in your project (not 100% sure off the top of my head which DLL the method resides in though, you'll have to look that up in the source).
Create your custom controller as Surface Controller, and obtain the IRoutableRequestContext,
public class propertydetailsController : SurfaceController
{
private IRoutableRequestContext _routableRequestContext;
}
then access the Dictionary helper class in Umbraco.Cms.Web.Dictionary, sample code is below.
DictionaryHelper dictionaryHelper = new DictionaryHelper(_routableRequestContext.Application);
string valueDictionary = dictionaryHelper.GetDictionaryItemValueForLanguage("DictionaryName", "en-GB");
Cheers

in Symfony, Frontend App, Using one of Module's form on another Module

I am building a project with Symfony. Its blog-like web site. I need to implement:
Writing comment for every article. Every comment must be moderationed by editors etc.
Everything is ready. I have a backend, use group, perms. so on. Just i need to comment form on article's show page.
My question is can i use my comment module's newSuccess temp. If yes, how? When i copy and paste the content of newSuccess, its not working evenif some conf.
Do you know there is way to use comment module's form at article module? and how can i configure it?
Thanks for spend your time to read -maybe to answer (;-
Just create the form in your controller:
public function executeShowArticle(sfWebRequest $request)
{
// assume weve already retrieved and set $this->article
$comment = new Comment();
$comment->setArticle($this->article);
$this->commentForm = new CommentForm($comment);
}
then you can use echo $commentForm in the template for your article. If you are customizing the layout of the comment form then move that form to a partial and do include_partial('comment/form', array('form' => $commentForm); from your article view. alternatively you could make a componet instead of using a straight partial... something like:
// in commentComponents.class.php
public function executeArticleCommentForm()
{
$comment = new Comment();
$comment->setArticle($this->article);
$this->form = new CommentForm($comment);
}
// in article/showArticleSuccess.php
<?php include_component('comment', 'articleCommentForm', array('article' => $article)); ?>

ruby on rails Controller class instantiate a ruby class?

I am new to RoR .I want my controller to instatiate an existing class from lib .
Collect data in the form of a string and throw the result on the view.erb.Can I do that.
Do i have to create a new model object and that should that model object inturn call the lib class.
Not really sure what you want to do.
If you used a library class - a module for example - its automatically instantiated, when you use 'include'
If you just have a generic class, and you included it somewhere, then you already have the class object loaded and can call methods on it. Or you just create an instance manually with 'object = new MyClass'.
And then call whatever you like on 'object'.
Whatever Information you collect inside the controller method, you can access in the view, when you place an '#'-Symbol before your variable.
So if you want your show.html.erb look like this:
<h1>My String:</h1>
<%= #mystring %>
then you have to do something like this in your controller:
def show
...
#mystring = MyClass.get_my_cool_string
...
end
Hope that helps...

Resources