Skip to content

pebezo/Portal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Portal

Portal is a ASP.NET MVC Razor view helper that allows you to send blocks of HTML, JavaScript, or CSS from a view or partial view to the layout view. This overcomes the current Razor view limitation of not being able to define sections in partial views.

For example, you could register all your JavaScript blocks using Portal and output everything in your layout before the closing of the body tag. Also, from your views or partial views you could list your CSS or JavaScript dependencies, calls to .css or .js files, without having to worry about ending up with duplicate file registrations.

With Portal, from a view or partial view, you add something into the portal using @Html.PortalInXXX, where XXX is one of the available methods. You then call the matching method @Html.PortalOutXXX in your layout.

The table below lists the methods available and a description of what they do. The code in the In column should go into the view or partial view, while the code in the Out column should go in the layout view.

In Out Description
@Html.PortalIn(text)
or
@Html.PortalIn(template)
@Html.PortalOut() Send any text (HTML, JavaScript, CSS, etc) through the default portal:
@Html.PortalIn("Hello World")
You can also send a Razor template:
@Html.PortalIn(@<text> $(function() { alert('Hi'); }); </text>)
And then somewhere in the layout view:
@Html.PortalOut()
@Html.PortalIn(key, text)
or
@Html.PortalIn(key, template)
@Html.PortalOut(key) Same as PortalIn(text / template) except that you can define a custom portal by specifying a key. The same key (string) must be used for the In and Out methods.
@Html.PortalInUnique(key, text)
or
@Html.PortalInUnique(key, template)
@Html.PortalOut(key) Same as PortalIn(key, text / template) except that if you try to add the same content twice only the first addition is actually added.
@Html.PortalInCss(path)
or
@Html.PortalInCssAbsolute(path)
@Html.PortalOutCss() Registers a path to a CSS file, for example:
@Html.PortalInCss("~/content/some.css")
The output in our example would be:
<link href="/content/some.css" rel="stylesheet" type="text/css" />
Duplicate paths are removed. In the layout file you should call PortalInCss for all the CSS files that may be added. This way if the same CSS file is registered from a different view then that CSS file would only be linked once in the layout.
If you would rather Portal not modify the given path or you would like to specify the full path, you can use:
@Html.PortalInCssAbsolute("http://example.com/css/my.css")
or
@Html.PortalInCssAbsolute("//example.com/css/my.css")
@Html.PortalInJs(path)
or
@Html.PortalInJsAbsolute(path)
@Html.PortalOutJs() Registers a path to a JS file, for example:
@Html.PortalInJs("~/scripts/my.js")
The output in our example would be:
<script src="/scripts/my.js" type="text/javascript"></script>
Duplicate paths are removed. If you don't want Portal to change the path you can use:
@Html.PortalInJsAbsolute("http://example.com/scripts/my.js")
@Html.PortalInStyle(text)
or
@Html.PortalInStyle(template)
@Html.PortalOutStyle() Registers a block of CSS in a view or partial view:
@Html.PortalInStyle(@<style type="text/css"> #some-id { font-weight: bold; }</style>)
The output is rendered without processing.
@Html.PortalInScript(text)
or
@Html.PortalInScript(template)
or
@Html.PortalInScriptUnique(text)
or
@Html.PortalInScriptUnique(template)
@Html.PortalOutScript() Registers a JavaScript block in a view or partial view, for example:
@Html.PortalInScript(@<script type="text/javascript"> $(function () { alert('Hi!'); }) </script>)
The output is rendered without processing. If PortalInScriptUnique is used, then duplicate blocks are not added.

Limitations

When Razor files are rendered the order is from sub partial views to views and then to the layout. For this reason you cannot have an In portal in the layout and an Out portal in a partial view; nothing gets called in the layout view until all the sub-views are rendered.

Within a layout, view, or partial view you can have an In and Out portal as long as the In portal is placed before the Out portal.

How to use

The only file you need is Portal.cs. If you're using NuGet to install Portal, the installer copies Portal.cs to /Helpers/Portal.cs in your main / current project, and modifies your web.config under /Views.

For manual installations, you can just copy Portal.cs to whatever folder you prefer and register the namespace in /Views/web.config like this:

<configuration>
	<system.web.webPages.razor>
		<pages>
			<namespaces>
				<add namespace="Portal"/>
			</namespaces>
		</pages>
	</system.web.webPages.razor>
</configuration>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published