Developer forum

Forum » Templates » Helpers, regex engine and mvc-like

Helpers, regex engine and mvc-like

Kim Pilgaard
Reply

We are in the process of moving our coding standard away from the old includes and towards helpers, functions and partials via the techniques described by Dynamicweb 
here https://doc.dynamicweb.com/forum/templates/razor-partial-views-and-helper-functions?PID=1605 .

During this process we have had a few issues, due to how the new include( "template", "namespace" ) function has been implemented.

1) If any typo has been made in "namespace" then everything looks fine in the IDE but an error is thrown on the website. This can get rather annoying when you have 
many includes of this type because you have categorized your helper functions into several files. This is off course due to the fact that the IDE assumes that the app_code 
folder works as "normal", hoever since Dynamicweb is mvc-like it is actually the regex engine which edits the file before the template engine renderes it.

2) @Include( "template", "namespace" ) does not work, you have to write exactly @Include("template","namespace"). Note the spacing, since this conflicted with our code 
style this caused a rather lengthy debugging session when our helpers no longer worked. This happens because the razor function which intellisense sees, and which is a 
standard razor function https://antaris.github.io/RazorEngine/references/razorengine-templating-templatebase-1.html, isn't what includes the file. That is done via the 
regex engine, with the spacing the regex engine does not catch the include and the razor function is fired which causes an error.

3) Judging by the parsed templates @Include("template","namespace") works by pasting the contents of the file into the tempalte and removing any references to the 
namespace. If this is correct then you would encounter issues if you have two helper files which have functions with the same signature.

We are fully aware that this is due to Dynamciweb being mvc-like and not mvc, as Nicolai mentioned in the previous forum post. However we are attempting to 
set a coding standard which utilizes Dynamicweb in the best possible manner while taking advantage of intelisense and allowing developers to search for information online.
When Dynamicweb uses a regex engine to overwrite what looks like legitimate razor code, then this causes confusion as to when we are writing code for razor and when 
we are writing code for the regex engine. Along the same train of thought it can be problematic to try and translate the razor/mvc information you find online 
(the two are usually connected) to dynamicweb mvc-like.

@Dynamicweb Can you tell us a bit about your future plans. Is the plan for you to go forward with mvc-like or can we look forward to real mvc?


Replies

 
Jesper Holm Damgaard
Reply

For starters it would be really nice if you could fix issue #2 so the DW Include function was space-insensitive.

--
All the best, Jesper

PS. +1 for MVC

 
Jesper Holm Damgaard
Reply

And now a little further down the road I am running into two new problems - one serious and one annoying.

The serious one:

When I try to assign the return value of  a function included with namespace inside Razor context the namespace is not replace when the template is compiled. I assume that it is because the namespace is not prefixed with @ and therefore missed by the regex.

@{

  string StringName = helpers.namespace.function();

}

 

The annoying one:

When including my global helpers and functions in a template, and then later in that template including a snippet-template, it must be included with the same namespace in order for the global functions to be available in the snippet. I guess that it has something to with the order in which the templates included, compiled and parsed.

 

Template.cshtml:

@Include("_inc/razor-functions-and-helpers.cshtml", "helpers.namespace")

@helpers.namespace.functionThatWillWork()

@Include("_inc/snippet.cshtml")

 

Snippet.cshtml:

@helpers.namespace.functionThatWillFail()

 

 
Nicolai Pedersen
Reply

Hi Kim and Jesper

This was just a quick try out during a forum thread - to give some quick and dirty intellisense. It has some things that could be added, so let's do that. You could consider to do most of these things in a regular dll - probably easier to accomplish exactly what you want.

@1: Yes, I have no solution to that.

@2: Simple fix - added to next hotfix, TFS#46866

@3: Understood. The alternative was to not do it. That is also doable?

In terms of "Real" MVC - you just go ahead all you want. Dynamicweb frontend is MVC already, and you are more than welcome to do whatever you want with the Dynamicweb.Frontend.PageViewModel. Or create your own models using Pageview/Items/Whatever. Be my guest! Dynamicweb just historically is also a rendering engine on top of the CMS - so moving to MS MVC only will leave a lot of features in the frontend not working, so we have chosen not to do that as of now.

It might be the route for DW10 - but currently not. It is also a compatibility issue...

Thank you for the feedback and Happy coding

Nicolai

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi Jesper and Kim

The problem regarding #46866 "Make spaces allowable in @include( "file", "namespace" )" has now been resolved in Dynamicweb version 9.4.3

To upgrade please choose this version from backend or download from here:

http://doc.dynamicweb.com/releases-and-downloads/releases

Let me know if you need any more help regarding this

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 

You must be logged in to post in the forum