Giter Club home page Giter Club logo

Comments (12)

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

Yeah the template name is not a relative path, you just need the name of the view you will be using as a template. The view engine will find the template for you. I usually put specific templates under the Shared directory. Additionally make sure are using the right extension.

try this.

Html.Flash(templateName: "Template")

Also what version of MvcFlash are you using, there is a V2 on nuget which has better templating abilities.

from mvcflash.

DeluxZ avatar DeluxZ commented on August 12, 2024

I'm using http://www.nuget.org/packages/MvcFlash.Core/. What you said to try out doesn't work.

Extension is *.cshtml.

Does the view engine search in all folders? Or just the root? Because I have the template nested in custom directories which isn't easy to change.

from mvcflash.

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

Oh ok, so you are looking at the wrong code. This is the code for MvcFlash version 1.

Try going here:

https://github.com/khalidabuhakmeh/MvcFlash2/blob/master/MvcFlash.Core/Extensions/FlashPusherExtensions.cs

https://github.com/khalidabuhakmeh/MvcFlash2/blob/master/MvcFlash.Core/Extensions/HtmlHelperExtensions.cs

You now specify the template on your Flash.Push call, and not the Html.Flash call.

Flash.Success("woot!", "you rocked it!", template: "Template");

Try that out. This method is a lot more flexible since the old way assumed all messages were of the same type, where as this one allows you to mix templates for success, error, warning, and info messages.

from mvcflash.

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

Also, if you look at the HtmlHelperExtensions, you'll notice there isn't much magic happening there, so feel free to write your own extension method to pass in a template automatically and override my behavior.

from mvcflash.

DeluxZ avatar DeluxZ commented on August 12, 2024

Ah I see. I made a basecontroller with methods so I can flash messages easier. It looks like this for the success message

public void SuccessMessage(string content, string template = "flash", string title = "", string id = "")
{
    Flash.Success(title, content, id, template);
}

As you can see my template is "flash", but how does your code find that template? I can't seem to find it in any of your classes. Neither in your HtmlHelperExtensions or FlashPusherExtensions

from mvcflash.

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

how does your code find that template?

It utilizes ASP.NET MVC's inherit ability to find views. The way the view engine works is it will look in the display templates folder, then the controller's view folder, then the shared folder.

The DisplayFor utilizes the type of the model, but can also be overridden by passing a template name.

You know the better approach is to create your own set of extension methods for specific message types you want.

public static void SuccessMessage(this IFlashPusher pusher) {
   pusher.Success("woot!", "Hooray!");
}

This allows you to reuse your code in places like FilterAttributes. Just a thought.

So is your problem solved? And how are you liking MvcFlash?

from mvcflash.

DeluxZ avatar DeluxZ commented on August 12, 2024

With the basecontroller it's working perfectly for our needs. The problem lays in our folder structure. We have a few packages which we can use for different projects and the MvcFlash stays in the Theme package. When we install this package it will install like this:

Project Root
-- Packages
---- Theme
------ Views
-------- Shared
---------- DisplayTemplates
------------ Flash.cshtml

With of course more folders, this is just to show where the Flash.cshtml file is located. If I copy the DisplayTemplates folder to the Shared folder in the Views folder in the !!root!! it works perfectly. We want to keep all the files in this Theme folder if possible.

And we like MvcFlash, works perfectly except we have this problem now with this package. For other projects without this package it works great.

from mvcflash.

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

So you need to modify your view engine to look in your Theme folder, it is really really easy to write a new view engine. Take a look at this view engine from from my other project Restful Routing.

https://github.com/stevehodgkiss/restful-routing/blob/master/src/RestfulRouting/ViewEngines/RestfulRoutingRazorViewEngine.cs

 public class RestfulRoutingRazorViewEngine : RazorViewEngine
    {
        public RestfulRoutingRazorViewEngine()
        {
            // {0} : Filename
            // {1} : Controller
            // {2} : Area
            AreaMasterLocationFormats = new[] {
                                                  "~/Views/{2}/{1}/{0}.cshtml",
                                                  "~/Views/{2}/{1}/{0}.vbhtml",
                                                  "~/Views/{2}/Shared/{0}.vbhtml",
                                                  "~/Views/{2}/Shared/{0}.cshtml",
                                              };

            AreaViewLocationFormats = new[] {
                                                "~/Views/{2}/{1}/{0}.cshtml",
                                                "~/Views/{2}/{1}/{0}.vbhtml",
                                                "~/Views/{2}/Shared/{0}.cshtml",
                                                "~/Views/{2}/Shared/{0}.vbhtml",
                                                // Potentially your new template folder
                                                "~/Packages/Theme/Views/{2}/Shared/{0}.cshtml",
                                                "~/Packages/Theme/Views/{2}/Shared/{0}.vbhtml",
                                            };

            AreaPartialViewLocationFormats = AreaViewLocationFormats;
        }
    }

All you need to do is list the the folders the view engine should know about. Play around with it. Restful routing has it's own view engine that is a nicer layout than the default view engines.

Leveraging a view engine makes it easier to change where MVC finds views for you. You can also make it so a view engine builds based on a database or external resource. The possibilities are endless.

from mvcflash.

DeluxZ avatar DeluxZ commented on August 12, 2024

Thanks for the detailed explanation! I tried to implement this in my project. I created a MvcFlashRazorViewEngine with the following code

public MvcFlashRazorViewEngine()
{
    // {0} : Filename
    // {1} : Controller
    // {2} : Area
    AreaMasterLocationFormats = new[] {
        "~/Packages/Launch.Theme/Views/Shared/DisplayTemplates/{0}.cshtml",
    };

    AreaViewLocationFormats = new[] {
        "~/Packages/Launch.Theme/Views/Shared/DisplayTemplates/{0}.cshtml",
    };

    AreaPartialViewLocationFormats = new[] {
        "~/Packages/Launch.Theme/Views/Shared/DisplayTemplates/{0}.cshtml",
    };
}

In my Application_Start in Global.asax in put this line

ViewEngines.Engines.Add(new MvcFlashRazorViewEngine());

I didn't clear my ViewEngines because I want to use the standard MVC View Engine + this custom one but I still get

Id
8a97d0b9a20f4ba0b967c8f7030fee6c
Title
Content
De gegevens van contentsoort zijn bijgewerkt.
MessageType
success
Template
flash

from mvcflash.

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

So you don't need the DisplayTemplates folder if I recall correctly. That is just assumed, what is most likely happening is you are getting doubly DisplayTemplate folders.

// wrong
"~/Packages/Launch.Theme/Views/Shared/DisplayTemplates/DisplayTemplates/Flash.cshtml",

try this

"~/Packages/Launch.Theme/Views/Shared/{0}.cshtml",

The best way to see what you are getting, is to override the FindPartial and FindView methods on your view engine and just put a breakpoint there.

You are close, but you just have to play around with it.

Here is the default view engine from ASP.NET MVC, notice no reference to DisplayTemplates folders.

 public RazorViewEngine(IViewPageActivator viewPageActivator)
      : base(viewPageActivator)
    {
      this.AreaViewLocationFormats = new string[4]
      {
        "~/Areas/{2}/Views/{1}/{0}.cshtml",
        "~/Areas/{2}/Views/{1}/{0}.vbhtml",
        "~/Areas/{2}/Views/Shared/{0}.cshtml",
        "~/Areas/{2}/Views/Shared/{0}.vbhtml"
      };
      this.AreaMasterLocationFormats = new string[4]
      {
        "~/Areas/{2}/Views/{1}/{0}.cshtml",
        "~/Areas/{2}/Views/{1}/{0}.vbhtml",
        "~/Areas/{2}/Views/Shared/{0}.cshtml",
        "~/Areas/{2}/Views/Shared/{0}.vbhtml"
      };
      this.AreaPartialViewLocationFormats = new string[4]
      {
        "~/Areas/{2}/Views/{1}/{0}.cshtml",
        "~/Areas/{2}/Views/{1}/{0}.vbhtml",
        "~/Areas/{2}/Views/Shared/{0}.cshtml",
        "~/Areas/{2}/Views/Shared/{0}.vbhtml"
      };
      this.ViewLocationFormats = new string[4]
      {
        "~/Views/{1}/{0}.cshtml",
        "~/Views/{1}/{0}.vbhtml",
        "~/Views/Shared/{0}.cshtml",
        "~/Views/Shared/{0}.vbhtml"
      };
      this.MasterLocationFormats = new string[4]
      {
        "~/Views/{1}/{0}.cshtml",
        "~/Views/{1}/{0}.vbhtml",
        "~/Views/Shared/{0}.cshtml",
        "~/Views/Shared/{0}.vbhtml"
      };
      this.PartialViewLocationFormats = new string[4]
      {
        "~/Views/{1}/{0}.cshtml",
        "~/Views/{1}/{0}.vbhtml",
        "~/Views/Shared/{0}.cshtml",
        "~/Views/Shared/{0}.vbhtml"
      };
      this.FileExtensions = new string[2]
      {
        "cshtml",
        "vbhtml"
      };
    }

from mvcflash.

DeluxZ avatar DeluxZ commented on August 12, 2024

Thanks! Overriding those two methods is awesome for debugging. I got it working now and I even made some other code a bit more clean thanks to this.

Thanks for the great support and fast responses. Learned something new today 👍

from mvcflash.

khalidabuhakmeh avatar khalidabuhakmeh commented on August 12, 2024

Awesome! Thanks for being patient and working through it. ViewEngines are pretty cool and can help reduce a lot of noise in your code.

If your site is going to be public could you send me a link? I'd love to see it.

P.S. Feel free to ask any questions if you need further assistance :)

from mvcflash.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.