Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Burrows 7 posts 77 karma points
    Jan 15, 2023 @ 14:30
    David Burrows
    0

    Include file within Razor cshtml file

    Hi,

    I'm wondering if there's a way to embed / include / import a cshtml file into another cshtml file, where both files are only being edited within the Umbraco v7 backend.

    A friend has an old v7 site where all the source code has been lost. They're spec'ing out a whole new site, but in the mean time has asked me to add content from a JSON API to content that's already on a few pages of the existing v7 site.

    We still have access to the backend and I've managed to do what I want using an @functions block that is copied on to each page requiring the new content. The new content gets added to a List along with the present content and then each page renders it slightly differently. I did it this way, so basically I'm messing with the Model only not Model and View. The rendering "code" remains exactly the same.

    It works OK, but the code is duplicated so is a pain.

    Is there anyway to have my @functions block in a separate file and then include it in each of the partials / pages that require it as though it was copied-n-pasted in?

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Jan 15, 2023 @ 15:18
    Huw Reddick
    0

    Hi David,

    I don't know if this would be possible in Umbraco, but worth a try

    https://www.c-sharpcorner.com/UploadFile/56fb14/reusable-methods-in-razor-view-using-inline-helper-method/

  • David Burrows 7 posts 77 karma points
    Jan 15, 2023 @ 15:27
    David Burrows
    0

    Hi Huw,

    That might well be the best way. It does involve adding a file into the App_Code directory via ftp (or something).

    Was trying to find a way to do it purely by the backend?

    Thanks Dave

  • David Burrows 7 posts 77 karma points
    Jan 15, 2023 @ 15:59
    David Burrows
    0

    Just as a test I tried ChatGPT with the same question and heres a link to the conversation. It sounds hopeful, but I think it may be getting Umbraco versions mixed up as I still haven't quite got it working.

Please Sign in or register to post replies

Write your reply to:

Draft